Route rename and redirect producing wrong URL helper

tuff

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'
  • which serves FelinesController#index at /cats - good
  • and redirects /felines to /cats - also good

But when debugging, I found that

  • new_feline_path is /cats/new - good
  • felines_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?

usha
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.

edited at
0

Comments

0 comments
Login to comment

Related