In order to use UJS in your Rails 3 app, you need to do the following:
Make sure you put the rails js tag after prototype. I wasted 30 minutes figuring that out.
Rails 3 makes it easy to use jQuery or Mootools if you prefer them over Prototype. Just include them instead.
2. Use the forgery protection meta tags: <%= csrf_meta_tag %>
Rails uses these to prevent XSS attacks. I don’t know the details, so I will not go into it.. but you need to have this helper which spits out 2 meta tags:
Don’t hard code these. The helper provides a unique token.
3. Change your link_to_remotes to link_to and use the :remote parameter
Lets say you have a list of widgets and people can vote Yes or No as to whether they like the widget. After they vote, it updates the box with a highlight and changes the text to show you the vote result. Here is my big ugly Rails 2 version of this:
Here is what you do in Rails 3 (please ignore the ugly path.. we are renovating):
You now use a regular link_to tag and pass the :remote parameter as true.
4. Use a js.erb file instead of render :text=>”blah blah”
The link_to tag does not accept :update, :complete, etc., like the link_to_remote tag did. You can drop in a js.erb file to handle this stuff instead. I determine the text and color of the highlight in my controller action. /myapp/app/views/widgets/vote.js.erb
Another jQuery example – http://joshhuckabee.com/jquery-rails-3