Override rails url helper method globally

341 Views Asked by At

I had a route that is used across my app as foo_path => /foo

Now I'm migrating this route to another app which defines the URL path differently /deep/path and also name the query_paramters used in the past by foo_path controller differently.

I'm looking to override the foo_path method so that now it returns /deep/path instead of /foo when called in view, controllers ... while transforming some query parameters names

I found some answer on stackoverflow but the method they use seem not to be working anymore on rails 4.2.7 which is the rails version I'm currently using.

How can I override foo_path method across the all app so that it returns /deep/path instead of /foo in rails 4.2.7 while being able to manipulate query_parameters ?

1

There are 1 best solutions below

1
Ziyan Junaideen On

What you need to do is to have a named route.

For example, lets say your old path was identified as foo and you accessed it as foot_path.

Then you can do some thing like this in routes.rb.

'/bar', to: 'controller#action', as: :foo

That way foo_path will now give /bar

ref: http://guides.rubyonrails.org/routing.html#overriding-the-named-helpers