Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rail Routes: Controller namespaces with constraints (subdomains)

The intention is to create a subdomain to hold all the administrative function (CRUD) and the name of the subdomain is "admin". The set of controllers responsible are also organized under the namespace of "admin", i.e. the controllers are under the app/controllers/admin directory.

Ideally, the following routes should be

admin.mydomain.com/products/      admin.mydomain.com/products/new ... 

and not

admin.mydomain.com/admin/products/ admin.mydomain.com/admin/products/new ... 

I would like to keep the helpers with the "admin" prefix such as:

new_admin_product edit_admin_product 

My current routing code works and it is as below:

constraints :subdomain => "admin" do   scope :module => "admin", :as => "admin" do     resources :players   end end 

Is this the right approach?

like image 744
Ronnie Liew Avatar asked Dec 22 '10 08:12

Ronnie Liew


People also ask

What is namespace in Ruby on Rails?

A namespace is a container for multiple items which includes classes, constants, other modules, and more. It is ensured in a namespace that all the objects have unique names for easy identification. Generally, they are structured in a hierarchical format so, that names can be reused.

What is namespace in Rails routes?

This is the simple option. When you use namespace , it will prefix the URL path for the specified resources, and try to locate the controller under a module named in the same manner as the namespace.

What are resources in Rails routes?

Any object that you want users to be able to access via URI and perform CRUD (or some subset thereof) operations on can be thought of as a resource. In the Rails sense, it is generally a database table which is represented by a model, and acted on through a controller.


1 Answers

Yes, this will give you precisely what you're after in the neatest fashion I know possible.

like image 154
Ryan Bigg Avatar answered Oct 14 '22 22:10

Ryan Bigg