2014-01-16 40 views
4

我試圖讓ActionController::UnknownFormat不會引發生產中的異常報告。我使用Rails 4,並認爲這樣的事情會做的伎倆,但它似乎不有所作爲:Rescue ActionController :: UnknownFormat引發異常

application.rb中

config.action_dispatch.rescue_responses.merge!('ActionController::UnknownFormat' => :not_found) 

回答

6

看起來這是在棄用Rails 4支持這種rescue_from語法。因此,像這樣:

application_controller.rb:

rescue_from ActionController::UnknownFormat, with: :raise_not_found 

    def raise_not_found 
    render(text: 'Not Found', status: 404) 
    end 
1

它不應該返回狀態代碼404,應該返回狀態碼415即unsupported_media_type

rescue_from ActionController::UnknownFormat, with: :raise_not_found 

def raise_not_found 
    render(text: 'Not Found', status: :unsupported_media_type) 
end 
相關問題