Urls generated by nested map.resources

Hi,

Apologies if this has been answered before.

I have just started to try out the new RESTful routes. For resources
that aren’t related to anything else, everything makes sense - for
resources with relationships, I am less certain of what the “right”
thing to do - especially in relation to the urls produced, and how to
modify those urls.

For example, if I have teams and roles - each role belonging to one
team and teams having many roles. Each role must belong to a team.

Currently my routes.rb looks something like this:

map.resources :team do |team|
team.resources :roles
end

map.resources :roles

The urls I think I want for roles are:

GET /roles # show all roles
GET /teams/1/roles # show all roles for team 1

GET /teams/1/roles/new # it only makes sense to create roles in the
context of a team
POST /teams/1/role

GET /roles/1 # show, edit, update or destroy role
1
GET /roles/1;edit
PUT /roles/1
DELETE /roles/1

To me it doesn’t make sense to have a url such as GET /teams/1/roles/1

  • the role id is the thing that uniquely identifies the role, so the /
    teams/1 is irrelevent. My understanding is that urls should be
    unambiguous - how would such a url be different from GET /roles/1 ?

So my questions are:

  1. Am I “right” or have I misunderstood something?
  2. The two map.resources entries in routes.rb seem to clash - when I
    try and call roles_path(@team) in my “new” template, rails complains -
    i.e. it won’t let me output POST /teams/1/role. I can get around this
    by using a hidden field in the “new” form, but this seems really
    hacky. Is there any other way around this?
  3. I don’t want the /teams/1/roles/1 urls - is there any way of
    limiting the routes that map.resources produces?
  4. will what I want to do break usage of ActiveResource?

On a side note, the things I have read about rest impy that the url is
a noun and you only have the four HTTP verbs available to you - I
don’t understand why urls such as /teams/1;edit are “better” than /
teams/edit/1 - both include a verb in the url - or is using the
semicolon just a convention to get around the limitation of only
having 4 verbs available to you?

Sorry for the long post - any thoughts + help greatly appreciated.

TIA
Roland

I have managed to solve the practical problem of having several routes
pointing to the same controller - you use the name_prefix to make the
routes work. Any thoughts on any of the other issues though? I still
have the /teams/3/roles/1 urls which I can’t seem to get rid of.

Cheers,
Roland

On Feb 20, 6:40 pm, “Roland S.” [email protected]