开发者

Rails 3.1 respond_to :html with :except

I have the following in my controller:

respond_to :html, :except => :some_action
respond_to :json, :xml

If you hit the :some_action route in a browser (tested with Chrome), you get a 406 Not Acceptable response back. Is there a way to "catc开发者_如何学JAVAh" this in Rails and do something else (like a redirect)?

Additionally, I'm trying to avoid using the block form of respond_to. I'm just curious if there is some way to handle this case.


Check this out: http://ryandaigle.com/articles/2009/8/6/what-s-new-in-edge-rails-cleaner-restful-controllers-w-respond_with

There's a bit about action overriding:

class UsersController < ApplicationController::Base

  respond_to :html, :xml, :json

  # Override html format since we want to redirect to a different page,
  # not just serve back the new resource
  def create
    @user = User.create(params[:user])
    respond_with(@user) do |format|
      format.html { redirect_to users_path }
    end
  end
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜