Search code examples
ruby-on-railsroutesnamespaces

Change namespace in Rails routes


I have namespaced routing like

/example/list/

namespace :example do
  resource :list
  ...
end

Need to change it to

/newexample/list/

I can certainly do it by changing example => newexample in routes.rb, but module example is used vastly in the project. so changing it by this method would require to update almost each Controller, Js, CSS file.

Is there any way to map newexample => example namespace in routes.rb? or Any other out of box solution?


Solution

  • You can specify a path when using a namespace with the path option. In your example you would have something like this:

    namespace :example, path: 'newexample' do
      resources :list
    end
    

    You can also specify the module instead, like so

    namespace :newexample, module: :example do
      resources :list
    end
    

    The routes created will be the same (/newexample/list will map to the example module).

    The difference will be in the route helper names: for the first example, your helper will be example_lists_path, in the second one it will look like newexample_lists_path (which means you would have to update your links in your views)

    You can also check the docs on scoping routes if you need more detailed info