可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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 "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?
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 "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
help!
回答1:
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
回答2:
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
<%= csrf_meta_tag %>
fixed things for me.
回答3:
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>
回答4:
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:
<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.
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.
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.
On a side note I did follow these instructions:
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.
回答5:
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.
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:
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 }
回答6:
This worked for me:
<%= link_to "Recommend", recommend_user_path(@user), :remote => true %>
Check that this is in your views\layout\application.html.erb (or equivalent):
<%= csrf_meta_tags %>
Note that Rails v3.2.2 uses "tags" not "tag"