Who Needs Actions

Ruby on Rails, Javascript and Friends.

jQuery Ajax With Rails Respond_to format.js

This year has been quite a shift for me in every aspect of my life – especially with regard to web development and this blog will now reflect that. For most part, my full time job and side projects use Ruby on Rails and related technologies such as jQuery, mysql, memcache, redis and other web development aspects such as xhtml, css, usability and design. Future posts will announce projects of my own, document my discoveries and occasionally broadcast my opinions on all that is Web Development. Follow me. Let’s jump straight in:

There’s many resources out there that detail how to use jQuery with Ruby on Rails, specially how to handle the returning of data and the setting of javascript acceptance headers. Ryan Bates for example has a Railscast on the topic - yet in my case, I was not wanting to serialize and submit a form. If you simply want to post data to the server and insert the returned generated html into your page – simply use the jQuery.ajax with dataType set to script. From here, you can simply render the partial within the controllers format.js block and all should work without the need for any prior jQuery setup.

To demonstrate, here’s two code snippets copied straight from an application of mine:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  # the jquery
  $.ajax({
    url: '/polls/' + poll_id + '/vote?option_id=' + option_id,
    dataType: 'script',
    type: 'POST',
    success: function(data) { $('#poll').html(data); }
  });

  # the controller action
  respond_to do |format|
    format.html { redirect_to '/' }
    format.js {
      render :partial => 'widget', :locals => { :poll => @poll }
    }
  end

Till next time!