Peel me a Grape :: We make things work

We Make Things Work :: Blog

respond_to allows us to emit different views using the same action:

def index
  @people = Person.find(:all)
  respond_to do |format|
    format.html
    format.xml { render :xml => @people.to_xml }
  end
end

In this case you could retrieve xml or html using the same url – by varying the ‘Accept’ header. But it turns out this header is used quite inconsistently across browsers, and the order of your format calls in the respond_to block can be significant. (Which isn’t obvious)

Disabling use of the Accept header is a welcome change.

Most people probably haven’t relied on it for a while. The prefered approach is to use formatted urls (eg. /people/1.xml or /people.js), which will still be routed to the same action – through the default route of /:controller/:action/:id.:format. or with map.resources.

Changes like these show how rails is evolving – correcting mistakes as it goes. A case study in continual improvement.

blog comments powered by Disqus