[英]Why isnt the the link to working in my js.erb file
I have this 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);
and it just hangs but when i comment out the link_to_funtion everything works great. 它只是挂起,但是当我注释掉link_to_funtion时,一切都很好。 I even tried to make this a link_to and it still hangs and does nothing ....am i missing something
我什至试图将其设置为link_to,但仍然挂起,什么也没做..am我错过了一些东西
Extract all of that to a partial, call it _cart_item.html.erb
: 将所有内容提取为部分内容,称为
_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>
Then, your js.erb
file will look like this: 然后,您的
js.erb
文件将如下所示:
$("#cart").append(<%= escape_javascript(render(:partial => "cart_item")) %>);
But Ryan Bigg is still right, you should: 但是Ryan Bigg仍然正确,您应该:
:remote => true
for AJAX requests, as I've done. :remote => true
。 id
to the destroy
action so it knows which object to destroy. id
传递给destroy
操作,以便它知道要销毁的对象。 Four things. 四件事。
One: No need for the :url
option. 一:不需要
:url
选项。 You can just pass the hash as the second argument to link_to_remote
and it'll work. 您可以将哈希作为第二个参数传递给
link_to_remote
,它将起作用。
Two: You should use the URL helpers for this. 二:您应该为此使用URL帮助器。 Also, if you're using the
destroy
action you'll probably want to pass through an id. 另外,如果您使用的是
destroy
动作,则可能需要传递一个id。 This means that you would do something like this instead: 这意味着您将改为执行以下操作:
link_to_remote 'Delete', registration_path(id)
You would be doing something like resources :registrations
for this. 您将为此做诸如
resources :registrations
的事情。 Although, not sure what registrations are within your application, so I can really advise on this. 尽管不确定您的申请中包含哪些注册,所以我可以就此提供真正的建议。
Three: most importantly, you're missing the end curly bracket for the hash on the link_to_remote
line. 第三:最重要的是,您缺少
link_to_remote
行上哈希的结尾大括号。 This would raise a syntax error if you left it out. 如果您将其遗漏,则会引发语法错误。
Four: link_to_remote
is deprecated in Rails 3.0. 四:在Rails 3.0中不推荐使用
link_to_remote
。 You should use link_to 'Delete', registration_path(id), :remote => true
您应该使用
link_to 'Delete', registration_path(id), :remote => true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.