I guess I slowly get the idea... With RESTful Rails you can only use
route structures that are logically tied to the models and their
relations, right?
(I have renamed my model MusicArtist to Artist in the meanwhile.)
class Music < ActiveRecord::Base
has_many :artists
end
class Artist < ActiveRecord::Base
belongs_to :music
end
In this case this will work:
map.resources :music do |music|
map.resources :artists
end
But this doesn't really make sense in my case. I want to be able to
structure it semantically, my models Music and Artist don't have any
relation to each other!
My website has different main parts, e.g. music, info etc. For each of
these main parts I have a controller: MusicController, InfoController
etc. When accessing
example.com/music
the MusicController delivers me with some common information about what
I can find in the sub sections of "music", e.g. a listing of music
artists in
example.com/music/artists
So the relation between MusicController (which doesn't even have its own
model!) and ArtistsController is only a semantically one, there's no
"real" relation between them!
So is it possible somehow to add the prefix "music/" to the routes of
Artist?
@Joshua -- REST, even in its Rails incarnation, does not have any
requirement about the naming of models/controllers or their
interaction. A resource, in the REST vernacular, is simply an
addressable object that understands how to respond to a particular set
of commands. Technically speaking, a resource could be a front for
one object (the Rails standard from script/generate scaffold) but you
could also have a resource that is composed of many objects (e.g., a
blog post with comments could be represented as a single resource) and
even one with no models (e.g., the Session from
restful_authentication).
In your particular case it really sounds like you want to look into
namespacing routes. This would allow you to create a hierarchy of
controllers (app/controllers/music, app/controllers/film) and have
separate controllers for each 'namespace'... but all of them could be
fronts for the same set of models. As Thorsten mentions, this is
often done with 'admin', allowing an index with edit links and an edit
page only for admins while the rest of the community may see only an
index and show.
map.namespace :music do |music|
music.resources :artists
music.resources :albums
end
map.namespace :film do |film|
film.resources :artists
film.resources :movies
end
map.namespace :music do |music|
map.resources :artists, :controller => 'music_artists'
end
I suggest you read David Black's eBook on Rails routing, The Rails Way
by Obie Fernandez, or read one of the various RESTful routing PDF's
out there. It will explain all of this.
Ah stupid typos on my part. Also, when you namespace, you need to
move them into their own directory. So, for example, you'd need to
move app/controllers/artists_controller.rb into
app/controllers/music/artists_controller.rb.
Then, this should work:
map.namespace :music do |music|
music.resources :artists
end
And you should get url's like 'music/artists' with the ability to have
something like 'painting/artists' too.
map.namespace :music do |music|
map.resources :artists, :controller => 'music_artists'
end
I suggest you read David Black's eBook on Rails routing, The Rails Way
by Obie Fernandez, or read one of the various RESTful routing PDF's
out there. It will explain all of this.