Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails namespace with path name different than module name

I'd like to have a namespace in Rails where the module name in code is different than the path name that user sees in the URL.

One can make a namespace in routes like so:

namespace :admin_ui do
  resources :posts
end

and this will match /admin_ui/posts to AdminUI::PostsController.

How can I make this namespace match the path /admin, but use module AdminUI?

like image 351
tybro0103 Avatar asked Jan 27 '14 18:01

tybro0103


People also ask

What is namespace in route in Rails?

Namespaces are used to organize a group of controllers. For example, you may wish to organize a group of controllers that are only accessible by the admin of your app, under the Admin:: namespace.

What is the difference between scope and namespace in Rails?

The difference lies in the paths generated. The paths are admin_posts_path and admin_comments_path for the namespace, while they are just posts_path and comments_path for the scope. You can get the same result as a namespace by passing the :name_prefix option to scope.

What is match in Rails routes?

Rails routes are matched in the order they are specified, so if you have a resources :photos above a get 'photos/poll' the show action's route for the resources line will be matched before the get line. To fix this, move the get line above the resources line so that it is matched first.


1 Answers

Found it:

namespace :admin_ui, path: 'admin' do
  resources :posts
end
like image 77
tybro0103 Avatar answered Nov 14 '22 23:11

tybro0103