Skip to content

Responding to AJAX requests

Ronald Chan edited this page Mar 20, 2012 · 5 revisions

By default, AJAX Pagination will not have any custom responses to AJAX requests. This means that the full webpage will be returned, and the client-side javascript will select only the content in the relevant div paginated section to display.

This is inefficient however, so it is recommended that a custom response is added, which only returns the necessary content.

AJAX response in a controller action

This can be done as shown below by adding a call to ajax_respond in the respond_to block of the appropriate action.

Class ObjectsController < ApplicationController
  def index
    ...
    respond_to do |format|
      format.html # index.html.erb
      ajax_respond format, :section_id => "page", :render => {:template => "objects/show"}
    end
  end
  ...

end

The :section_id option should be set to the AJAX section id.

The other option is :render, which is the template which should be returned. In this case, show.html.erb. Notice that the render option takes a hash of options passed directly to the render function, but with the layout automatically set to false (unless set explicitly to true). A view partial can also be selected by passing a {:partial => "page"} hash for example. In this case the default behaviour is to render the _page.html.erb partial if it exists, because the :section_id option is "page". If it does not exist, the usual default template is rendered (but without the layout). If a string is passed to the render option, it is as if the hash passed was {:action => "string"}.

AJAX response for all controller actions

Sometimes, there may be navigation between the different actions of a controller, or site-wide navigation between controllers.

To make it easy to add a custom response, without explicitly specifying this in every single action, there is another method by the same name, but as a class method. It takes the same options as for the instance method.

It works by rendering the custom AJAX response for AJAX Pagination requests as a default render behaviour. This means that the behaviour can still be overridden by any explicit action-level response logic.

To use it, just use something like the following:

class ApplicationController < ActionController::Base
  ajax_respond :section_id => "global"

  ...
end

Here, the :render option is omitted, which means that the default behaviour is used for selecting the view template to render. It can also be set explicitly to change, or overriden at the action-level if there are exceptions for certain actions.

This can also be used for a specific controller, instead of the ApplicationController.

This class method can also be used for specific actions, or excluding certain actions, for example:

class ApplicationController < ActionController::Base
  ajax_respond :section_id => :internal, :only => [:index,:show]        # using symbols
  ajax_respond :section_id => "global", :except => ["update","create"]  # is identical to using strings
  ...
end