Rails通过Ajax销毁js错误

时间:2022-02-20 19:38:18

Okay so I'm building a really simple list with items app, pretty much exactly the same as your standard to-do list application. I've managed to ajax-ify the creation of new 'points' within a list (point belongs_to :list and list has_many :points) but I'm having trouble with the 'destroy' action.

好的,所以我正在使用items app构建一个非常简单的列表,与标准待办事项列表应用程序非常相似。我已经设法ajax-ify在列表中创建新的'points'(点belongs_to:list并列出has_many:points)但是我遇到了'destroy'操作的问题。

When I click on the destroy link in the browser, nothing visibly occurs, and I get the error Error: Syntax error, unrecognized expression: /lists/10/points/125 obviously with different values depending on the id of the list and point.

当我点击浏览器中的destroy链接时,没有任何明显的现象,我得到错误错误:语法错误,无法识别的表达式:/ lists / 10 / points / 125显然具有不同的值,具体取决于列表和点的ID。

If I refresh the page or look at the db, it's clear that the entry has indeed been deleted. Without ajax, my destroy action works just fine. I feel like I must be missing something obvious, any ideas?

如果我刷新页面或查看数据库,很明显该条目确实已被删除。没有ajax,我的销毁行动就可以了。我觉得我必须遗漏一些明显的,任何想法?

fyi the 'pro' attribute is just a boolean associated with every point.

fyi'pro'属性只是与每个点相关联的布尔值。

points_controller.rb

def destroy 
  @point = @list.points.find(params[:id])
  @point.destroy
  respond_to do |format|
    format.html { redirect_to list_url(@list) }
    format.js
  end
end

lists/show.html.erb

 <% @list.points.each do |point| %>
      <% if point.pro == true and point.valid? == true %>
        <li class="weight-<%= point.weight %>"><%= point.content %>
          <%= link_to "&times;".html_safe, [@list, point],  
                  :remote => true,
                  :method => :delete, 
                  :class=> "close", 
                  :data => {:dismiss => 'alert'} %>
    </li>

And it doesn't seem to matter what I put in views/points/destroy.js.erb, because the code doesn't seem to be getting executed.

并且我在views / points / destroy.js.erb中放置的内容似乎并不重要,因为代码似乎没有被执行。

Update

I figured it out, I had to change the path in the delete link to list_point_url(@list, point). The other problem was that my invalid javascript was causing a server error, so I didn't realize what the problem was (turns out #<%= dom_id(@point) %> needed to be wrapped in quotes).

我想通了,我不得不将删除链接中的路径更改为list_point_url(@ list,point)。另一个问题是我的无效javascript导致服务器错误,所以我没有意识到问题是什么(原来#<%= dom_id(@point)%>需要包含在引号中)。

Thanks all!

1 个解决方案

#1


0  

Maybe check if the delete link routes to the destroy controller action, because list_point_path doesn't really seem like a delete route.

也许检查删除链接是否路由到destroy控制器操作,因为list_point_path看起来不像删除路由。


Edit

Sorry for the lake of knowledge but I'm not sure what [@list, point] will produce as a route. This is what I have for a view of my own, just for your reference:

对不起知识之湖,但我不确定[@list,point]会产生什么样的路线。这就是我对自己的看法,仅供您参考:

link_to "Delete", admin_photo_path(photo), :method => :delete, :confirm => "Delete this image?", :class => "btn-trash"

My admin_photo_path is a singular path that route to a single Photo instance; not a collection.

我的admin_photo_path是路由到单个Photo实例的单一路径;不是一个集合。

Edit

Simple way could be sending delete to the point object, maybe this could help?

简单的方法是将删除发送到点对象,这可能会有所帮助吗?

link_to "&times;".html_safe, point,  
              :remote => true,
              :method => :delete, 
              :class=> "close", 
              :data => {:dismiss => 'alert'}

#1


0  

Maybe check if the delete link routes to the destroy controller action, because list_point_path doesn't really seem like a delete route.

也许检查删除链接是否路由到destroy控制器操作,因为list_point_path看起来不像删除路由。


Edit

Sorry for the lake of knowledge but I'm not sure what [@list, point] will produce as a route. This is what I have for a view of my own, just for your reference:

对不起知识之湖,但我不确定[@list,point]会产生什么样的路线。这就是我对自己的看法,仅供您参考:

link_to "Delete", admin_photo_path(photo), :method => :delete, :confirm => "Delete this image?", :class => "btn-trash"

My admin_photo_path is a singular path that route to a single Photo instance; not a collection.

我的admin_photo_path是路由到单个Photo实例的单一路径;不是一个集合。

Edit

Simple way could be sending delete to the point object, maybe this could help?

简单的方法是将删除发送到点对象,这可能会有所帮助吗?

link_to "&times;".html_safe, point,  
              :remote => true,
              :method => :delete, 
              :class=> "close", 
              :data => {:dismiss => 'alert'}