Forum: Ruby on Rails restful nested and unnested routes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ab3250bbbcd06b8b817cab8a9398ef13?d=identicon&s=25 Sebastian Schön (Guest)
on 2007-06-11 21:06
(Received via mailing list)
Hi,

I´ve problems with the restful routes.

I know that /artists/1/songs works:

  map.resources :artists do | artists |
     artists.resources :songs
   end

But how can I route to a list of all songs with artist, something
like  /songs ... in a restful manner.

Thanks
Sebastian
A887cc76c61e1f4148caacb5f32361d6?d=identicon&s=25 jparker (Guest)
on 2007-06-12 19:21
(Received via mailing list)
You could create a separate top-level song resource to accomplish
this, e.g.,

map.resources :artists do |artist|
  artist.resources :songs, :name_prefix => 'artist_'
end
map.resources :songs

This would create routes for a top-level song resource as well as your
original nested song resource. The top-level song resource will have
the usual route helpers. Because of the :name_prefix option, the route
helpers for the nested song resource will have "artist_" prepended to
their name to prevent them from conflicting with the top-level song
resource, e.g., artist_songs_path(X) => /artists/X/songs.

-John
This topic is locked and can not be replied to.