Rajan Verma - Aarvy
Rajan Verma - Aarvy

Reputation: 2117

Alias name for nested resources rails routes

I have batch_class model, and under that articles, assignments resources are there. The routes.rb look like this.

resources :batch_classes do
  member do 
    resources :articles, except: [:index] 
    resources :assignments
 end

end

The problem is I am not getting desired URL route. My rake routes for edit assignments shows this.

/batch_classes/:id/assignments/:id/edit(.:format)

with alias name of edit_assignment_path. However, my expected route is

/batch_classes/:batch_class_id/assignments/:id/edit(.:format).

Please help.

Upvotes: 1

Views: 675

Answers (2)

Imran Ahmad
Imran Ahmad

Reputation: 2927

The desired path route can be achieved by taking resources :assignments out of the member block.

resources :batch_classes do
  resources :assignments
  member do 
    #...
  end
end

Going through this document is highly recommended for correct implementation.

Upvotes: 1

quyetdc
quyetdc

Reputation: 1585

You can have nested routes by just putting nested resource inside parent resources.

resources :batch_classes do
  resources :assignments
end

Then, you surely will have a route called edit_batch_class_assignment_path and you can pass in instances as edit_batch_class_assignment_path(@batch_class, @assignment)

Upvotes: 3

Related Questions