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?
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Daniel Talsky 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. :) 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. :)
--Jeremy
On 10/1/07, Daniel Talsky
<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:>
> 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?
> --
> Posted via http://www.ruby-forum.com/.
>
> >
>
--
http://www.jeremymcanally.com/
Ruby in Practice:
http://www.manning.com/mcanally/
My free Ruby e-book:
http://www.humblelittlerubybook.com/book/
My blogs:
http://www.mrneighborly.com/
http://www.rubyinpractice.com/
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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 => 1What 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---