繁体   English   中英

为什么不链接到我的js.erb文件中

[英]Why isnt the the link to working in my js.erb file

我的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,但仍然挂起,什么也没做..am我错过了一些东西

将所有内容提取为部分内容,称为_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操作,以便它知道要销毁的对象。

四件事。

一:不需要:url选项。 您可以将哈希作为第二个参数传递给link_to_remote ,它将起作用。

二:您应该为此使用URL帮助器。 另外,如果您使用的是destroy动作,则可能需要传递一个id。 这意味着您将改为执行以下操作:

link_to_remote 'Delete', registration_path(id)

您将为此做诸如resources :registrations的事情。 尽管不确定您的申请包含哪些注册,所以我可以就此提供真正的建议。

第三:最重要的是,您缺少link_to_remote行上哈希的结尾大括号。 如果您将其遗漏,则会引发语法错误。

四:在Rails 3.0中不推荐使用link_to_remote 您应该使用link_to 'Delete', registration_path(id), :remote => true

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM