Routing Nested Resources in Rails 3

Routing nested resources in Rails 3

The best way to do this depends on the application, but in my case it is certainly Option B. Using namespaced routes I'm able to use a module to keep different concerns separated out into different controllers in a very clean way. I'm also using a namespace-specific controller to add shared functionality to all controllers in a particular namespace (adding, for example, a before_filter to check for authentication and permission for all resources in the namespace).

Rails nested resources and routing - how to break up controllers?

All you are doing with nested resources is changing the routing URL. Only thing you would have to do is make sure you are passing the proper id (in your case post)to the tag controller. Most common error is the Can't Find *** ID.

If you don't nest a profile route into a user route it would look like this

domain.com/user/1

domain.com/profile/2

When you nest the routes it would be

domain.com/user/1/profile/2

That is all that it is doing and nothing else. You don't need additional controllers. Doing nested routing is just for looks. allowing your user to follow the association. The most important thing about nesting routes is that you make sure you make the link_to's to the right path.

When not nested: it would be

 user_path

and

 profile_path

when it is nested you would need to use

user_profile_path

rake routes is your friend to find out how the routes have changed.

Hope it helps.

Rails 3 Nested Routing

Ok as stated above, I would recommend doing something like this:

def index
if params[:company_id]
@events = Company.find(params[:company_id]).events
else
@events = Events.all
end
end

although if you need to you can specify a controller:

resources :companies do
resources :events, :controller => "companies/events"
end
resources: events

and just create a companies folder inside your controllers folder to put your Companies::EventsController inside

How to route multiple levels of nested resources in Rails 3?

You can keep nesting the way you have shown and things will work fine. There are quite a few sources that will tell you not to go crazy nesting routes though. Take a look at Rails Best Practices for example (I think the article was created for rails 2 but the principals still apply). Jamis Buck also blogged about this a while ago.

rails - how to target nested controller for nested resource route

Definitely you can!

Here you need to customize your resourceful route by explicitly specifying a controller to use for the resource. The :controller option will let you do that.

So, in your case, specifying the controller as clients/products for the admin_clients_products resource would work in your desired way.

namespace :admin do
resources :clients, only: [:index] do
resources :products, only: [:index, :new, :create], controller: 'clients/products'
end # ------------------------------

resources :products, only: [:index]
end

rails routes will now give you what you want:

admin_client_products    GET /admin/clients/:client_id/products(.:format)   admin/clients/products#index
POST /admin/clients/:client_id/products(.:format) admin/clients/products#create
new_admin_client_product GET /admin/clients/:client_id/products/new(.:format) admin/clients/products#new
admin_clients GET /admin/clients(.:format) admin/clients#index
admin_products GET /admin/products(.:format) admin/products#index

=========================

Extra bits:

If you want to omit the /admin portion from the url (I mean, if your application's routing design permits to), then you could use:

scope module: 'admin' do...end
like the following:

scope module: 'admin' do
resources :clients, only: [:index] do
resources :products, only: [:index, :new, :create], controller: 'clients/products'
end

resources :products, only: [:index]
end

and suddenly your routes will start looking awesome :)

client_products     GET    /clients/:client_id/products(.:format)     admin/clients/products#index
POST /clients/:client_id/products(.:format) admin/clients/products#create
new_client_product GET /clients/:client_id/products/new(.:format) admin/clients/products#new
clients GET /clients(.:format) admin/clients#index
products GET /products(.:format) admin/products#index

Rails 3 nested routes with constraints

As far as I know, when you set a constraint on a parent route, the child route will inherit the constraint on that field. Thus, my understanding is that:

UUID_regex = /([a-z0-9]){8}-([a-z0-9]){4}-([a-z0-9]){4}-([a-z0-9]){4}-([a-z0-9]){12}/
resources :posts, :only => [:index, :show, :create], :constraints => {:id => UUID_regex} do
resources :comments, :only => [:create, :destroy]
end

is sufficient. Is this not the case? My apps are still in 3.1/1.9.2, so haven't tested in a 3.2 app.

Rails nested resource routing error

You need to change your form code, page_path is called from it. Should be

<%= form_with(model: [ @user, @page ]) do |form| %>

In this case the route will be set correctly.

grouping controller in subdirectories for nested resources

If you want to use just that one route:

match 'locations/:location_id/users' => "locations/users#index"

That should come before any other resources/matches that might conflict with that match. By default Rails routes are top-bottom.

# should be before locations resource
resources :locations do
resources :users
end

Alternatively, if you want to punt all your nested users resource over to locations/users you can assign a controller to the resource.

resources :locations do
resources :users, :controller => "locations/users"
end


Related Topics



Leave a reply



Submit