atmorell
atmorell

Reputation: 3068

Nested routing

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

Upvotes: 1

Views: 177

Answers (3)

Michael Sofaer
Michael Sofaer

Reputation: 2947

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'

Upvotes: 4

Carlos Lima
Carlos Lima

Reputation: 4172

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 :)

Upvotes: 1

Ethan
Ethan

Reputation: 60169

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"}

Upvotes: 1

Related Questions