views:

5154

answers:

4

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!

+3  A: 

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

mckeed
hehe, thanks for the tip with the typo. unfortunately, that was not the problem but the new unobtrusive javascript helpers need a meta tag include. thanks for the pointer though! ;)
z3cko
+2  A: 

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://www.themodestrubyist.com/2010/02/24/rails-3-ujs-and-csrf-meta-tags/

 <%= csrf_meta_tag %>

fixed things for me.

z3cko
A: 

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", "#", :onlick => "alert('Hello world!');"
=> <a href="#", onclick="alert('Hello world!');">name</a>
netbe
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.

robeastham
By the way I found the solution from this page:http://featurenotbug.com/2009/05/getting-jquery-and-prototype-to-work-together-in-rails/
robeastham