我的 js.erb 文件中有这个
var cart_item = "<table id='something' style='width:100%'>";
cart_item += "<tr><td>";
cart_item += "<%= @contact.name %>";
cart_item += "<%= link_to_remote 'Delete', :url => {:controller => 'registrations', :action => 'destroy' %>";
cart_item += "</td>";
cart_item += "</tr></table>";
$("#cart").append(cart_item);
它只是挂起,但是当我注释掉 link_to_funtion 时,一切都很好。我什至尝试将其设为 link_to,但它仍然挂起并且不执行任何操作...我是否缺少某些内容
最佳答案
将所有内容提取到一个部分,并将其命名为_cart_item.html.erb
:
<table id='something' style='width:100%'>
<tr><td>
<%= @contact.name %>
<%= link_to 'Delete', {:controller => 'registrations', :action => 'destroy', :id => @contact.id}, :remote => true %>
</td>
</tr></table>
然后,您的 js.erb
文件将如下所示:
$("#cart").append(<%= escape_javascript(render(:partial => "cart_item")) %>);
但是 Ryan Bigg 仍然是对的,你应该:
- 使用 RESTful 路由和路由助手。
- 对 AJAX 请求使用
:remote => true
,就像我所做的那样。 - 确保您将
id
传递给destroy
操作,以便它知道要销毁哪个对象。
关于javascript - 为什么我的 js.erb 文件中没有工作链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9287248/