Fri
Dec
26
merb's provides/display in rails
DHH posted about merging merb’s provides/display into rails. He proposed used a dual of respond_to and respond_with, similar to merb’s provides/display, with a couple tweaks to allow each action to respond to different types. There are a couple things about the API he suggested which I don’t love:- I don’t like the way respond_with(@deals, :to => [:html, :json, :xml]) reads, why not flip that around and do respond_to(:html, :json, xml, :with => Deal.new). This would work very well with how the current responds_to method works in rails, just adding an :with option to allow converting that object instead of rendering a partial. (see the index action below)
- When using the class-level respond_to, we could use respond_to(:with => Deal.new), or keep an alias of repond_with(Deal.new) (see the new action below)
- It would be nice to have an easy way to use the defaults for some response types, but allow customization for others. (see the create action below)
class DealsController < SubjectsController
respond_to :html, :xml, :json
def index
@deals = Deal.all
respond_to(:html, :xml, :json, :atom, :with => @deals)
end
def new
respond_to(:with => Deal.new)
# uses the class-level defined response types (html, xml, and json)
# maybe this is a good case for also having respond_with(Deal.new)
end
def create
@deal = Deal.create!(params[:deal])
respond_to(:xml, :json, :with => @deal) do |format|
format.html { redirect_to @deal }
end
end
end