views:

56

answers:

3

Suppose I have a Book model, which contains many Page models.

The routing for this would be as so:

map.resources :books do |book|
    book.resources :pages
end

Following the Rails default on this quickly leads to problems. Suppose Book #1 has 10 pages. The first Page in Book #2 will have this route:

/books/2/pages/11

This is a pretty bad route, what would make more sense is this:

/books/2/pages/1

Or even this:

/books/2/1

Is there a way to still use map.resources, but get a result like this:

/books/{book.id}/pages/{page.page_number}
+1  A: 

No. You have to use custom routing for that.

Feel free to get inspiration from http://github.com/augustl/kii/blob/master/config/routes.rb

August Lilleaas
Wow, that is some crazy routing. Looking at that will definitely help, thanks!
Karl
+1  A: 

As August says you need to use custom routing for that. But for the pages, you don't need the full resources routes. Only show will be necessary.

So something like :

map.resources :books do |book|
    book.page ':page_id', :action => 'index'
end

Will map the default books url for displaying the index, one book and adding/editing them. But also a page

/books/{book.id}/{page_id}

Which maps to the index action with the parameter "page_id". You only have to display the appropriate books page ;)

Damien MATHIEU
A: 

You could also try the shallow-option for your routing!

Lichtamberg