Skip to content

Add meta data (like pagination info) to your response

fabrik42 edited this page Sep 24, 2011 · 4 revisions

Add meta data to your response

Sometimes you will need to add information to the data of your response that don't have a place in your api templates. The most common use case is probably the information about paginated data.

acts_as_api makes it easy to add meta data to your response by passing a :meta option to the render_for_api method in the controller.

Example

def index
  meta_data = { :page => params[:page], :total => User.count }

  respond_to do |format|
    format.json { render_for_api :public, :json => @users, :root => :users, :meta => meta_data }
  end
end

Will return something like:

{
  "users": {
    // user data
  },
  "page": 1,
  "total": 1325
}