Jquery ·

Get jQuery Working With Ruby on Rails 3

I thought I had fixed everything and made ready for Rails 3 in my applications. Turns out I was wrong. My wife tells me I’m wrong all the time so I didn’t feel to bad. The first sign was that something bad had happened was that all my delete links stopped working. I found a couple of articles talking about the new unobtrusive javascript in Rails.

I have bits of code that look like this:


<%= link_to 'Delete Thing', @thing, :confirm => 'Are you sure?', :method => :delete %>
</pre>

I know the argument that you shouldn't use links for delete. In this case the generated anchor tag won't cause you problems so I don't want to hear any whining:


<a href="algebra" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Delete Course</a>
</pre>

I included rails.js which will turn those links into DELETE requests for the server. However, I started seeing this error in Firebug on Firefox:

document.on is not a function
</pre>

I also got this in Google Chrome:


Uncaught TypeError: Object #<htmldocument> has no method 'on'
</pre></code>

Turns out that since I use jQuery I needed to get the rails.js file for jQuery. There's a gem that wraps it up:
https://github.com/rails/jquery-ujs

You can also just get the javascript and dump it in rails.js:
<a href="https://github.com/rails/jquery-ujs/raw/master/src/rails.js">https://github.com/rails/jquery-ujs/raw/master/src/rails.js</a>

Now I can be happy again.
</htmldocument>