Generate 404 error
I created a action called error404
in the controller PagesController
. I would like to display this action if the following situation occurred:
- The controller in URL does not exist
- The action in URL does not exist
How can I accomplish this?
I tried to follow the instruc开发者_运维技巧tions on this page, Show a 404 instead of 500 in Rails , but that only seems to work with the first situation.
Thanks for help. :)
I think if action in your controller is not existed and your controller is due to the scaffold generation then by default it goes to the 'show' method of a controller.
For Ex;:- http://www.domain.com/controller_name/invalid_action_name
Here your "controller_name" is valid Controller. &
"invalid_action_name" is invalid action.
Then by default it goes to the 'show' method of "controller_name" & considered "invalid_action_name" as a params[:id].
so in show method you can rescue the error and can show the valid error page such as "Record not found" or "error 404"
EDITED you have to add route for a new method name in your /config/routes.rb as follows
map.resources :controller_name, :collection=>{:show=>:get}
don't forget to restart a server whenever you change this file.
精彩评论