嵌套路由

时间:2009-07-03 21:13:21

标签: ruby-on-rails ruby

如何编写映射此类路径的路由?

/高级用户/鲍勃/文章标题

这是我到目前为止所得到的:

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

这给了我以下路线:

/高级用户/:USER_ID //:ID

我如何摆脱双重反击? /超级用户/管理员//第一文章?

祝你好运。 AsbjørnMorell

3 个答案:

答案 0 :(得分:4)

好的,如果您不想要中间嵌套资源(/ articles),我根本不会使用map.resources。

尝试:

map.connect '/powerusers/:user_id/:article_title', :controller => 'articles', :action => 'view_by_title'

答案 1 :(得分:1)

如果我添加......

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

我得到以下路线,包括你想要的路线......

(为您的情况替换“条目”的“文章”。)

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

答案 2 :(得分:1)

而不是嵌套,这会起作用吗?

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

我认为它不会,但快速测试会更好地说明:)