views:

610

answers:

3

I have a [somewhat] restful route setup with:

map.resources :forgotten_passwords,
              :as => 'forgotten-passwords',
              :except => [:destroy, :show, :delete, :index]
   forgotten_passwords POST   
   /forgotten-passwords(.:format) 
   {:action=>"create", :controller=>"forgotten_passwords"}

   new_forgotten_password GET    
   /forgotten-passwords/new(.:format) 
   {:action=>"new", :controller=>"forgotten_passwords"}

   edit_forgotten_password GET    
   /forgotten-passwords/:id/edit(.:format) 
   {:action=>"edit", :controller=>"forgotten_passwords"}

   forgotten_password PUT    
   /forgotten-passwords/:id(.:format) 
   {:action=>"update", :controller=>"forgotten_passwords"}

If a visitor accesses /forgotten-passwords via GET they are presented with a blank page. The log however shows an exception "ActionController::MethodNotAllowed: Only post requests are allowed."

I would like to redirect to another action/view and display a pleasant error message to the visitor in this case. I think the visitors are clicking a link in an email that cuts off the end of the url.

I realize I could add a GET route to the create and then handle the error in the controller, but something tells me there's a better way.

A: 

Check this similar post: Rails: Displaying a user friendly message for a routing error in rails

Also this general blog post: Custom 404 action in Rails

And this one for other types of Action Controller exceptions: Action Controller: Rescue

khelll
+5  A: 

Explicitly disabling :index from your routes means the /forgotten-passwords GET request is disabled. I you remove that you should have a functioning index method again.

Within the index method you can create a page that explains the situation.

tadman
This is exactly how you do it.
Brian Hogan
A: 

Wouldn't http://api.rubyonrails.org/classes/ActiveSupport/Rescuable/ClassMethods.html#M001002 be a better solution?

squil