使用 rails3 (link_to_remote) 和基本 javascript 的 link_to 语法在 rails3 应用程序中不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2458841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
link_to syntax with rails3 (link_to_remote) and basic javascript not working in a rails3 app?
提问by z3cko
i am wondering if the basic link_to syntax is completely broken in current rails3 master or if i am doing some wrong syntax here.
我想知道基本的 link_to 语法在当前的 rails3 master 中是否完全被破坏,或者我在这里做了一些错误的语法。
= link_to "name", nil, :onlick => "alert('Hello world!');"
should actually produce an alert on click. very simple. does not work on my rails3 project! (also no error output!) any ideas?
实际上应该在点击时产生警报。很简单。不适用于我的 rails3 项目!(也没有错误输出!)有什么想法吗?
for the general link_to syntax i could not find an example where i could combine a link_to_remote with a confirmation, remote and html class (see my try below)
对于一般的 link_to 语法,我找不到可以将 link_to_remote 与确认、远程和 html 类结合的示例(请参阅下面的尝试)
= link_to "delete", {:action => "destroy", :remote => true, :method => :delete, :confirm => "#{a.title} wirklich Löschen?" }, :class => "trash"
even the rails3 api does not help me here: http://rails3api.s3.amazonaws.com/index.html
即使 rails3 api 在这里也帮不了我:http: //rails3api.s3.amazonaws.com/index.html
help!
帮助!
回答by mckeed
I believe your problem here is that you've set the link up to show the alert when it is licked, as opposed to when it is clicked. ;)
我相信您的问题是您已将链接设置为在被舔时显示警报,而不是在单击时显示警报。;)
As for link_to_remote, it has changed with the switch to unobtrusive javascript. You can read about it here: http://blog.solnic.eu/2009/09/08/unobtrusive-javascript-helpers-in-rails-3.html
至于link_to_remote,随着切换到不显眼的 javascript,它发生了变化。你可以在这里阅读:http: //blog.solnic.eu/2009/09/08/unobtrusive-javascript-helpers-in-rails-3.html
回答by z3cko
ok it looks like the new unobtrusive javascript changes introduced the problem. see the following post for more information if you run into similar issues http://blog.loopedstrange.com/modest-rubyist-archive/rails-3-ujs-and-csrf-meta-tags
好的,看起来新的不显眼的 javascript 更改引入了问题。如果您遇到类似问题,请参阅以下帖子以获取更多信息 http://blog.loopedstrange.com/modest-rubyist-archive/rails-3-ujs-and-csrf-meta-tags
<%= csrf_meta_tag %>
fixed things for me.
为我固定的东西。
回答by netbe
nil doesn't work:
零不起作用:
= link_to "name", nil, :onclick => "alert('Hello world!');"
=> <a href="/currentpath", onclick="alert('Hello world!');">name</a>
You should use:
你应该使用:
= link_to "name", "#", :onclick => "alert('Hello world!');"
=> <a href="#", onclick="alert('Hello world!');">name</a>
回答by robeastham
If none of the other answers here work for you then maybe this will help.
如果这里的其他答案都不适合您,那么这可能会有所帮助。
So the csrf_meta_tag declaration was not enough for me, though should be added in your layout file for Rails 3 anyway. With me it turned out to be a conflict with jQuery. I just put this:
所以 csrf_meta_tag 声明对我来说还不够,尽管无论如何应该添加到 Rails 3 的布局文件中。对我来说,结果是与 jQuery 发生了冲突。我只是把这个:
<script type="text/javascript">
jQuery.noConflict();
</script>
after the rails scripts tag in my layout and the clash between Prototype and jQuery was resolved. Hey presto I was getting the confirmation dialog box on delete.
在我的布局中的 rails 脚本标记以及 Prototype 和 jQuery 之间的冲突得到解决之后。嘿,我收到了关于删除的确认对话框。
This technique also resolved my original issue when using link_to to try and delete a record. With link_to any destroy command seemed to redirecting to the show page for the record. Hence I moved to button_to based on some other solution I saw, but with no confirmation. I wonder if there are some more deep seated issues with jQuery and Prototype.
该技术还解决了我在使用 link_to 尝试删除记录时的原始问题。使用 link_to 任何销毁命令似乎都重定向到记录的显示页面。因此,我根据我看到的其他一些解决方案转移到 button_to,但没有得到确认。我想知道 jQuery 和 Prototype 是否存在一些更深层次的问题。
This all happened on an upgraded Rails 2.3.5 app that seemed to be working okay without needing to include Prototype or :defaults in my layout file.
这一切都发生在升级后的 Rails 2.3.5 应用程序上,该应用程序似乎工作正常,无需在我的布局文件中包含 Prototype 或 :defaults。
On a side note I did follow these instructions:
在旁注中,我确实遵循了以下说明:
http://webtech.union.rpi.edu/blog/2010/02/21/jquery-and-rails-3/
http://webtech.union.rpi.edu/blog/2010/02/21/jquery-and-rails-3/
to try and lose Prototype all together for this project and use the jQuery git submodule for Rails 3 instead. Following these instructions did not work and I was still without confirm dialogs with button_to and the show page when using link_to. Just thought I'd mention it so save someone the trouble of trying this.
尝试将 Prototype 一起用于此项目,并改用 Rails 3 的 jQuery git 子模块。遵循这些说明不起作用,并且在使用 link_to 时,我仍然没有使用 button_to 和显示页面的确认对话框。只是想我会提到它,这样就可以省去尝试这个的麻烦。
回答by clemensp
An off-topic comment for an answer above since I can't comment yet :(
对上述答案的题外评论,因为我还不能发表评论:(
@robeastham: I figured I'd leave some comments that may help with some of the issues you've encoutered.
@robeastham:我想我会留下一些评论,这些评论可能有助于解决您遇到的一些问题。
I've been having the "destroy link re-directing to the show page instead of the index page" issue as well since switching from ActiveRecord to Mongoid, and haven't found a real solution (already have prototype removed). However, a workaround that works cleanly is to explicitely specify the re-direct path using :location to respond_with:
自从从 ActiveRecord 切换到 Mongoid 以来,我也一直遇到“销毁链接重定向到显示页面而不是索引页面”的问题,并且还没有找到真正的解决方案(已经删除了原型)。但是,一种有效的解决方法是使用 :location 来显式指定重定向路径以响应:
respond_with @themodel, :location => themodels_url
As for getting a confirmation pop-up with a button, you could do:
至于使用按钮获得确认弹出窗口,您可以执行以下操作:
button_to "Button Name", { :controller => "your/controller", :action => :action_name, :confirm => "Text for the pop-up"}, { :method => :<method name> }
e.g.
例如
button_to "Click Here", { :controller => "home", :action -> :set_completed, :confirm => "Mark the item complete?" }, { :method => :put }
回答by Nigel Sheridan-Smith
This worked for me:
这对我有用:
<%= link_to "Recommend", recommend_user_path(@user), :remote => true %>
<%= link_to "Recommend", recommend_user_path(@user), :remote => true %>
Check that this is in your views\layout\application.html.erb (or equivalent):
检查这是否在您的 views\layout\application.html.erb(或等效项)中:
<%= csrf_meta_tags %>
<%= csrf_meta_tags %>
Note that Rails v3.2.2 uses "tags" not "tag"
请注意,Rails v3.2.2 使用“标签”而不是“标签”

