REST: nested resources and attributes as resources

Hi --

map.resources :projects do |projects|   projects.resources :iterations end

But� what if I also want to list all the iterations (http://localhost:3000/iterations)? I can't, because a project id is required.

you can put this in your routes twice:

map.resources :projects do |projects| projects.resources :iterations end projects.resources :iterations

then use project_iterations_path(@project_id) or iterations_path()

as needed

In Rails versions < 2.0, you need to specify a name prefix in the nesting:

   map.resources :projects do |p|      p.resources :iterations, :name_prefix => "projects_"    end    map.resources :iterations

The reason is that <mapper>.resources :iterations creates named routes -- i.e., Ruby methods -- for you: iterations_path, iterations_url, etc. If you don't use a name prefix, then the second time you create those methods, the first ones you created get clobbered. It's just like doing:

   def iterations_path    end

   def iterations_path    end

The name prefix gives you sessions_iterations_path, which can peacefully coexist with iterations_path.

In Rails 2.0 the name prefix based on the outer resource is added automatically.

David

David, When Rails 2.0 automatically adds the 'parent' prefix is it singular or plural? Based on your answer, I'm going to go into my HUGE project and rewrite all of my routing to be ready for 2.0 Thank you, Kathleen

Hi --

Hi --

map.resources :projects do |projects|   projects.resources :iterations end

But... what if I also want to list all the iterations (http://localhost:3000/iterations)?I can't, because a project id is required.

you can put this in your routes twice:

map.resources :projects do |projects| projects.resources :iterations end projects.resources :iterations

then use project_iterations_path(@project_id) or iterations_path()

as needed

In Rails versions < 2.0, you need to specify a name prefix in the nesting:

   map.resources :projects do |p|      p.resources :iterations, :name_prefix => "projects_"    end    map.resources :iterations

The reason is that <mapper>.resources :iterations creates named routes -- i.e., Ruby methods -- for you: iterations_path, iterations_url, etc. If you don't use a name prefix, then the second time you create those methods, the first ones you created get clobbered. It's just like doing:

   def iterations_path    end

   def iterations_path    end

The name prefix gives you sessions_iterations_path, which can peacefully coexist with iterations_path.

In Rails 2.0 the name prefix based on the outer resource is added automatically.

David, When Rails 2.0 automatically adds the 'parent' prefix is it singular or plural? Based on your answer, I'm going to go into my HUGE project and rewrite all of my routing to be ready for 2.0 Thank you, Kathleen

I disclaim all legal responsibility for the results :slight_smile: It appears to be singular. Here's a console session with 2.0 release candidate 1:

   >> irb ActionController::Routing::Routes    >> draw do |map| map.resources :outers do |o| o.resources :inners;    ?> end; end    => [ActionController::Base, ActionView::Base]    >> puts named_routes.names.map {|r| r.to_s }.sort    edit_outer    edit_outer_inner    formatted_edit_outer    formatted_edit_outer_inner    formatted_new_outer    formatted_new_outer_inner    formatted_outer    formatted_outer_inner    formatted_outer_inners    formatted_outers    new_outer    new_outer_inner    outer    outer_inner    outer_inners    outers

This comports with the idea that every inner, or collection of inners, is scoped to a particular outer.

David