views:

99

answers:

3
+1  Q: 

Nested routing

Hey,

How do I write a route that maps a path like this?

/powerusers/bob/article-title

This is what I got so far:

map.resources :users, :as => "powerusers" do |users|
  users.resources :articles, :as => ''
end

This gives me the following route:

/powerusers/:user_id//:id

How do I get rid of the double backslah? /powerusers/admin//first-article?

Best regards. Asbjørn Morell

+4  A: 

Ok, if you don't want the intermediate nested resource (/articles) I wouldn't use the map.resources at all.

Try:

map.connect '/powerusers/:user_id/:article_title', :controller => 'articles', :action => 'view_by_title'
Michael Sofaer
Gives me: /powerusers/:user_id/admin/:id(.:format) {:controller=>"articles", :action=>"show"} :/
atmorell
Sweet :) Thanks a lot.
atmorell
Glad I could help!
Michael Sofaer
+1  A: 

If I add...

  map.resources :users, :as => "powerusers" do |users|
    users.resources :entries, :as => 'article-title'
  end

I get the routes below, which include the one you want...

(Substitute "articles" for "entries" for your situation.)

                GET    /powerusers(.:format)                                 {:controller=>"users", :action=>"index"}
                POST   /powerusers(.:format)                                 {:controller=>"users", :action=>"create"}
                GET    /powerusers/new(.:format)                             {:controller=>"users", :action=>"new"}
                GET    /powerusers/:id/edit(.:format)                        {:controller=>"users", :action=>"edit"}
                GET    /powerusers/:id(.:format)                             {:controller=>"users", :action=>"show"}
                PUT    /powerusers/:id(.:format)                             {:controller=>"users", :action=>"update"}
                DELETE /powerusers/:id(.:format)                             {:controller=>"users", :action=>"destroy"}
   user_entries GET    /powerusers/:user_id/article-title(.:format)          {:controller=>"entries", :action=>"index"}
                POST   /powerusers/:user_id/article-title(.:format)          {:controller=>"entries", :action=>"create"}
 new_user_entry GET    /powerusers/:user_id/article-title/new(.:format)      {:controller=>"entries", :action=>"new"}
edit_user_entry GET    /powerusers/:user_id/article-title/:id/edit(.:format) {:controller=>"entries", :action=>"edit"}
     user_entry GET    /powerusers/:user_id/article-title/:id(.:format)      {:controller=>"entries", :action=>"show"}
                PUT    /powerusers/:user_id/article-title/:id(.:format)      {:controller=>"entries", :action=>"update"}
                DELETE /powerusers/:user_id/article-title/:id(.:format)      {:controller=>"entries", :action=>"destroy"}
Ethan
This is the url I am looking for: /powerusers/:user_id/:id(.:format) I want to hide the second controller name - article-title in your example
atmorell
+1  A: 

Instead of nesting, would this work?

map.resources :users, :as => "powerusers"
map.resources :articles, :path_prefix => '/powerusers/:user_id'

I think it won't but a quick test would tell better :)

Carlos Lima
Just tried it and the result is /powerusers/:user_id/articles/:id(.:format) {:controller=>"articles", :action=>"show"} I am trying to get rid of article from the url.
atmorell
I'm just throwing ideas here (not that i know what im talking about). Have you tried :as => nil instead of :as => '' ?
Carlos Lima
np :) Jep, tried with :as => nil / false.
atmorell
was going to suggest you use regular map.connect then instead of users.resources .. but Michal Sofaer already did. Did it work?
Carlos Lima
It sure did :) Thanks.
atmorell