开发者

Sophisticated route and url generation in Rails

开发者 https://www.devze.com 2023-03-06 10:29 出处:网络
I have a group model which has_many article model. And I want to use the following url pattern \"{group_id}/{article_id}\".

I have a group model which has_many article model. And I want to use the following url pattern "{group_id}/{article_id}".

so I wrote these route codes:

resou开发者_如何转开发rce :groups do
  resource :articles
end
match ':group_id/:id(.:format)', :to => 'articles#show', :as => :article
match ':id', :to => 'groups#show', :as => :group

But rails fail to generate correct url for group records and article records. How can I replace the automatic generated article_path and group_path to match my routes?


You're running into problems because you aren't watching out for pluralization. When you define a singular resource route, Rails doesn't treat it as a collection where you would refer to each member with an id. You instead want a plural resources for both groups and articles:

resources :groups do
  resources :articles
end

Generates the following routes:

    group_articles GET    /groups/:group_id/articles(.:format)          {:action=>"index", :controller=>"articles"}
                   POST   /groups/:group_id/articles(.:format)          {:action=>"create", :controller=>"articles"}
 new_group_article GET    /groups/:group_id/articles/new(.:format)      {:action=>"new", :controller=>"articles"}
edit_group_article GET    /groups/:group_id/articles/:id/edit(.:format) {:action=>"edit", :controller=>"articles"}
     group_article GET    /groups/:group_id/articles/:id(.:format)      {:action=>"show", :controller=>"articles"}
                   PUT    /groups/:group_id/articles/:id(.:format)      {:action=>"update", :controller=>"articles"}
                   DELETE /groups/:group_id/articles/:id(.:format)      {:action=>"destroy", :controller=>"articles"}
            groups GET    /groups(.:format)                             {:action=>"index", :controller=>"groups"}
                   POST   /groups(.:format)                             {:action=>"create", :controller=>"groups"}
         new_group GET    /groups/new(.:format)                         {:action=>"new", :controller=>"groups"}
        edit_group GET    /groups/:id/edit(.:format)                    {:action=>"edit", :controller=>"groups"}
             group GET    /groups/:id(.:format)                         {:action=>"show", :controller=>"groups"}
                   PUT    /groups/:id(.:format)                         {:action=>"update", :controller=>"groups"}
                   DELETE /groups/:id(.:format)                         {:action=>"destroy", :controller=>"groups"}

If you want to leave off the groups and articles segments you can pass :path => '' to each of the resources definitions, but you are going to have to tread carefully because any request to http://example.com/1/2 will map to an article under groups and be uninformative to end users and bots alike.

0

精彩评论

暂无评论...
验证码 换一张
取 消