views:

266

answers:

1

I create a action called "error404" in controller "pages", I would like to display this action if the following situation occurred:

  1. the controller in URL is not existed
  2. the action in URL is not existed

what should I do?

I tried to follow the instruction in this page, http://stackoverflow.com/questions/2553365/show-a-404-instead-of-500-in-rails , but seems only work with first situation.

Thanks for help. :)

A: 

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.

Salil
Thanks. I am new to RoR and not quite understand.I created a new action called "show". When I hit the invalid_action_name, it didn't go into the action "show". It just give me....Unknown actionNo action responded to invalid_action_name. Actions: xxxx, xxxx, xxxIs that I made anything wrong?
siulamvictor
you have to add route to your routes.rb. please look into my Edited Answer.
Salil
oops. it's not working for me. but thanks for your help anyway. :)
siulamvictor