3

I have 2 resources, one being a nested resource of another:

parent_resource and child_resource.

This gives me the following routes:

somesite.com/parent_resources/14
somesite.com/parent_resources/14/child_resources/1

However there is only ever going to be a single child_resource for each parent_resource, so to someone using the site this is very confusing. I would like the child_resource paths to look like this:

somesite.com/parent_resource/14/child_resource
somesite.com/parent_resource/14/child_resource/edit
etc

What is the correct way to do this?

My routes.rb

resources :parent_resources do

   resource :child_resource do
   end

end 

From the rails guide to routing:

A singular resourceful route generates these helpers:

new_geocoder_path returns /geocoder/new
edit_geocoder_path returns /geocoder/edit
geocoder_path returns /geocoder

But what about show?

My routes generated by rake routes:

parent_resource_child_resource      POST   /parent_resources/:parent_resource_id/child_resource(.:format)                 child_resources#create


new_parent_resource_child_resource  GET    /parent_resources/:parent_resource_id/child_resource/new(.:format)             child_resources#new

edit_parent_resource_child_resource GET    /parent_resources/:parent_resource_id/child_resource/edit(.:format)            child_resources#edit

                                    GET    /parent_resources/:parent_resource_id/child_resource(.:format)                 child_resources#show

                                    PUT    /parent_resources/:parent_resource_id/child_resource(.:format)                 child_resources#update

                                    DELETE /parent_resources/:parent_resource_id/child_resource(.:format)                 child_resources#destroy
Undistraction
  • 42,754
  • 56
  • 195
  • 331
  • You should pass a plural name to `resources` and a singular name to `resource`. You're passing singular to both. – Rob Davis Jul 02 '12 at 17:32
  • Ah, I see you've corrected the names. And please verify that the named route doesn't work instead of relying solely on `rake routes` which may sometimes omit named routes that work. Because this scheme works fine for me. – Rob Davis Jul 02 '12 at 17:40

1 Answers1

8

In your routes, define the child resource using the singular resource method:

resources :parent_resources do
  resource :child_resource
end

By convention, the controller for the child will still be ChildResourcesController, plural.

Rails has a pretty good guide to routing. See the section on singular resources.

Rob Davis
  • 15,597
  • 5
  • 45
  • 49
  • Thanks. Problem is that this doesn't generate a named route for the child_resource#show. I only get a create, an edit and a new. – Undistraction Jul 02 '12 at 16:40
  • Yes you do. If your parent is "user" and your child is "profile", you should have `user_profile_path(@user)` – Rob Davis Jul 02 '12 at 16:56
  • I only have the following paths shown using rake routes:parent_resource_child_resources POST /parent_resources/:parent_resource_id/child_resources(.:format) child_resources#create new_parent_resource_child_resources GET /parent_resources/:parent_resource_id/child_resources/new(.:format) child_resources#new edit_parent_resource_child_resources GET /parent_resources/:parent_resource_id/child_resources/edit(.:format) child_resources#edit – Undistraction Jul 02 '12 at 17:02
  • Also, notice that the singular resource method should take a singular name. The plural "child_resources" in your output seems wrong. – Rob Davis Jul 02 '12 at 17:09
  • Good spot. I had accidentally changed it to the plural. Unfortunately when I switch it back I am left with only the same three routes (just singular). parent_resource_child_resource(@parent_resource) yields an undefined method error from the page which contains it. – Undistraction Jul 02 '12 at 17:21
  • I've pasted my routes into the question. – Undistraction Jul 02 '12 at 17:27