Routes both nested and RESTful

Most of the material on custom routes and URL generation in Rails
assumes non-nested, non-RESTful routes it seems. Especially for
non-crud actions it is difficult to find good examples for nested
resources.

RESTful tutorials show the basic CRUD routes, and some examples, but
scaffolding doesn’t generate example code that takes these routes into
account.

Non RESTful url creation for example is easy:
:url => { controller: controller_name, :action => action_name, :id => ID
}

generates:
controller_name/action_name/ID

But what about nested and restful resources like this:
parent_controller_name/parent_ID/controller_name/ID;action_name

How do I generate these? Do I have to construct and hard-code the URL
path myself?

Also, the functional controller tests refer to a simple action like
this:
get :new

How do I handle calling the “new” action for a nested resource?

Daniel T. wrote:

But what about nested and restful resources like this:
parent_controller_name/parent_ID/controller_name/ID;action_name

How do I generate these? Do I have to construct and hard-code the URL
path myself?

In routes.rb you can define extra actions to go with your resource, and
you’ll get those helpers for free. Look up the specifics on :collection
and :member.

Also, the functional controller tests refer to a simple action like
this:
get :new

How do I handle calling the “new” action for a nested resource?

You need to pass the IDs for the parent resources like so:

get :new, :parent_id => 1


Roderick van Domburg
http://www.nedforce.com

I’ll just show you an example. Let’s say you wanted to nest a city
inside of a state, like:

http://localhost/states/1/cities/3/

To nest the resources, you need to…nest them…in routes. :slight_smile: Like
this:

map.resources :states do |state|
state.resources :cities
end

Then, to add custom methods to each member (for example, if you wanted
to show the population for a city or something), then you would do
something like this:

map.resources :states do |state|
state.resources :cities, :member => { :population => :get }
end

…where population is the action name and get is the HTTP method you
want to allow to be used on it. You could also do collection in the
place of member to allow things like
http://localhost/states/3/cities/average_population or something.

Keep in mind that when you nest resources, though, it also requires
you call url_for/link_to with nesting. For example, rather than…

city_path the_city_id

…you need to do something like this:

state_city_path the_state_id, the_city_id

This also applies to custom methods.

Hopefully that will give you enough to get going. :slight_smile:

–Jeremy

On 10/1/07, Daniel T. [email protected] wrote:

Non RESTful url creation for example is easy:
path myself?


http://www.jeremymcanally.com/

Ruby in Practice:

My free Ruby e-book:
http://www.humblelittlerubybook.com/book/

My blogs:
http://www.mrneighborly.com/
http://www.rubyinpractice.com/

On Oct 2, 4:20 am, Roderick van Domburg <rails-mailing-l…@andreas-
s.net> wrote:

You need to pass the IDs for the parent resources like so:

get :new, :parent_id => 1

What about with namespaces where there is no :parent_id? In my views,
I would just pass :admin, but I’m not sure what to pass over in my
tests. Maybe
:admin => :admin
or
:admin => nil

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs