How to redirect when ActionNotFound in Rails
I have a controller SubscriptionsController
with only actions new
and create
. How would I redirect to new
if say someone tries to visit GET /subscriptions
w开发者_如何学运维hich would normally trigger the index
action?
config/routes.rb
resource :subscriptions, :only => [:new, :create]
Using rails3 you can do it from a route, something like:
match "/subscriptions", :to => redirect("/subscriptions/new")
Edit:
From the comments it was made clear you want to capture more than that, using a wild card you can make it more generic. You may need to combine this form with the previous to deal with the non-slash form (or try the below form without a slash, I havent tried that). Also make sure to put these "catch all" routes below your other ones since routes are matched from top to bottom.
match "/subscriptions/*other", :to => redirect("/subscriptions/new")
精彩评论