Stefanie Price
Stefanie Price

Reputation: 1

determining format of response in rails

When I 'GET /pages.js' rails routes the request like this:

{ :controller => 'pages', :action => 'index', :format => 'js' }

I'm looking for the code that determines the format to return to the client which I assume is in action_dispatch but I'm having a difficult time locating it.

Upvotes: 0

Views: 208

Answers (1)

Michael De Silva
Michael De Silva

Reputation: 3818

consider

def index
  @posts = Post.all
  respond_to do |format|
    format.html # index.html.erb
    format.js  { render :json => @posts}
  end
end

There's another extension to the above which lets you render a template to customise the json response; same goes for xml. Hope this helps.

Cheers, Mike.

Upvotes: 1

Related Questions