Reputation: 2533
I have setup custom error pages in my rails app.
application.rb
config.exceptions_app = self.routes
routes.rb
get '404', to: 'application#page_not_found'
get '422', to: 'application#server_error'
get '500', to: 'application#server_error'
application_controller.rb
def page_not_found
respond_to do |format|
format.html { render template: 'errors/not_found_error', layout: 'layouts/application', status: 404 }
format.all { render nothing: true, status: 404 }
end
end
def server_error
respond_to do |format|
format.html { render template: 'errors/internal_server_error', layout: 'layouts/error', status: 500 }
format.all { render nothing: true, status: 500}
end
end
My custom 500 error page shows up fine when I do a GET request that throws the error but when I submit a form that triggers a NoMethodError, so a PUT request, I just get a blank page.
Any ideas why the 500 error page displays correctly for a GET request but not for a PUT request?
I tried changing the server_error
method to
def server_error
render template: 'errors/internal_server_error', layout: 'layouts/error', status: 500
end
but this didn't seem to help.
Let me know if I can provide more code, I'm not sure how to troubleshoot this.
Upvotes: 2
Views: 826
Reputation: 3603
use match
and via
on your routes.rb to route all types of HTTP requests to custom error actions
# error routes
match '/404' => 'application#page_not_found', :via => :all
match '/422' => 'application#unprocessable_entity', :via => :all
match '/500' => 'application#server_error', :via => :all
Upvotes: 5