views:

50

answers:

1

Hi! Here is my Rails 3 nested routes structure for PROJECT

resources :projects do
  resources :notes, :photos
    collection do
        get 'yourproject', 'newjs'
    end
end

This works great for things like

/projects
/projects/1
/projects/1/notes/
/projects/1/notes/3

what isn't working is:

/projects/1/notes/newjs

Anyone Rails 3 nested resource experts out there? thanks

+1  A: 

You're missing a do in there, so it's defining routes like /projects/newjs It should be

resources :projects do
  resources :notes, :photos do
    collection do
      get 'yourproject', 'newjs'
    end
  end
end
Ben Taitelbaum
btw, `rake routes` is quite helpful for debugging
Ben Taitelbaum
You are seriously my hero.
AnApprentice
Actually wait! That didn't work right... close though... that ended up making a newjs_project_notes_path and a newjs_project_photos_path... When it should have made just "newjs_project_note_path" notice it pluraized notes and it should just be note?
AnApprentice
I think `newjs_project_notes_path` is correct, because we've said it's a collection, so we would expect several notes. Similarly, the notes index is `project_notes`, not `project_note` (`project_note` is reserved for getting a specific note by id)
Ben Taitelbaum
@Ben, but having a newjs_photos makes no sense, right? Maybe there is a different way in Rails 3 to setup the path newjs_project_note_path?
AnApprentice
newjs_photos doesn't make sense because photos is a nested resource, but newjs_project_photos makes sense -- I would assume there are several photos that I can access at /projects/:project_id/photos/newjs(:.format)
Ben Taitelbaum
If you wanted just a single photo labeled with newjs, you could either us a matcher, or have a db migration create a photo with id 'newjs'
Ben Taitelbaum
It really comes down to what you're trying to model. When you have nested resources and then a collection, you're saying that /projects/:project_id/notes/newjs is a special list of notes for any project. If you're trying to use it for a different purpose, then it's possible a simple matcher may work better: ` match '/projects/:project_id/notes/newjs.(:format)' => 'controller#action', :as => :newjs_project_note`
Ben Taitelbaum