link_to syntax with rails3 (link_to_remote) and basic javascript not working in a rails3 app?

大城市里の小女人 提交于 2019-11-29 08:04:40

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

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.

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>

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.

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 }

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"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!