I am trying to change the route of /felines
within my app to /cats
, and I would like to do this while making as few changes to my app code as possible (i.e. not renaming model, controller, or references to URL helpers).
In routes.rb
I am doing
get '/felines', to: redirect('/cats')
resources :cats, as: 'felines'
FelinesController#index
at /cats
- good/felines
to /cats
- also goodBut when debugging, I found that
new_feline_path
is /cats/new
- goodfelines_path
is /felines
- BAD!I also found that felines_path
will become /cats
, as desired, if I switch the order of the two lines in routes.rb
.
Why does the order of those lines affect the value of felines_path
?
Is there a better way I could achieve the re-routing and redirection?
get '/felines', to: redirect('/cats')
rails generates felines_path
route helper for the above route and since the priority is from top to bottom, felines_path
will point to /felines. That is the reason why it points to /cats
when you change the order.
Assign a different name for your custom route so it doesn't override your felines
path
get '/felines', to: redirect('/cats'), as: 'felines_redirect' #or someother name like that
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments