Friday, August 17, 2012

Simple AJAX on Rails


Basic ideas:
  • On server side:
    1. In routes.rb, setup routings to handle ajax request calls.
    2. In controller, handle ajax request by something like: respond_to do |format| format.js end
    3. In views, create *.js.erb file which has the same name as the corresponding controller function.
  • On client side:
    1. In view pages, use tags like <%= button_to , ..., :remote => true, ... %>
    2. Trigger the ajax request.  E.g. by $('#ajax_submit').submit();
    3. Create a javascript like fn_some_callback() to handle callback from ajax response.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.