2012-04-21 39 views
2

我正在使用Ruby on Rails v3.2.2,我想爲嵌套資源使用複數名稱。也就是說,在我的config/routes.rb我有(注:「類」和「文章」的樣本資源):如何使用複數名稱嵌套資源?

resources :categories do 
    resources :articles do 
    collection do 
     get 'one' 
     post 'two' 
     put 'three' 
    end 
    member do 
     get 'four' 
     post 'five' 
     put 'six' 
    end 
    end 
end 

上述語句生成以下內容:

$ rake routes 
    one_category_articles GET /categories/:category_id/articles/one(.:format)  articles#one 
    two_category_articles POST /categories/:category_id/articles/two(.:format)  articles#two 
three_category_articles PUT /categories/:category_id/articles/three(.:format) articles#three 
    four_category_article GET /categories/:category_id/articles/:id/four(.:format) articles#four 
    five_category_article POST /categories/:category_id/articles/:id/five(.:format) articles#five 
    six_category_article PUT /categories/:category_id/articles/:id/six(.:format) articles#six 
     category_articles GET /categories/:category_id/articles(.:format)   articles#index 
         POST /categories/:category_id/articles(.:format)   articles#create 
    new_category_article GET /categories/:category_id/articles/new(.:format)  articles#new 
    edit_category_article GET /categories/:category_id/articles/:id/edit(.:format) articles#edit 
     category_article GET /categories/:category_id/articles/:id(.:format)  articles#show 
         PUT /categories/:category_id/articles/:id(.:format)  articles#update 
         DELETE /categories/:category_id/articles/:id(.:format)  articles#destroy 
      categories GET /categories(.:format)        categories#index 
         POST /categories(.:format)        categories#create 
      new_category GET /categories/new(.:format)       categories#new 
      edit_category GET /categories/:id/edit(.:format)      categories#edit 
       category GET /categories/:id(.:format)       categories#show 
         PUT /categories/:id(.:format)       categories#update 
         DELETE /categories/:id(.:format)       categories#destroy 

我想改變報表在我config/routes.rb所以產生以下路由器與category_article「部分」(即,我想用categories_article/分別爲,而不是category_article/category_articles):

$ rake routes 
# Note: I marked changes from the previous outputting with '=>'. 
=> one_categories_articles GET /categories/:category_id/articles/one(.:format)  articles#one 
=> two_categories_articles POST /categories/:category_id/articles/two(.:format)  articles#two 
=> three_categories_articles PUT /categories/:category_id/articles/three(.:format) articles#three 
=> four_categories_article GET /categories/:category_id/articles/:id/four(.:format) articles#four 
=> five_categories_article POST /categories/:category_id/articles/:id/five(.:format) articles#five 
=> six_categories_article PUT /categories/:category_id/articles/:id/six(.:format) articles#six 
=>  categories_articles GET /categories/:category_id/articles(.:format)   articles#index 
          POST /categories/:category_id/articles(.:format)   articles#create 
     new_category_article GET /categories/:category_id/articles/new(.:format)  articles#new 
     edit_category_article GET /categories/:category_id/articles/:id/edit(.:format) articles#edit 
      category_article GET /categories/:category_id/articles/:id(.:format)  articles#show 
          PUT /categories/:category_id/articles/:id(.:format)  articles#update 
          DELETE /categories/:category_id/articles/:id(.:format)  articles#destroy 
        categories GET /categories(.:format)        categories#index 
          POST /categories(.:format)        categories#create 
       new_category GET /categories/new(.:format)       categories#new 
       edit_category GET /categories/:id/edit(.:format)      categories#edit 
        category GET /categories/:id(.:format)       categories#show 
          PUT /categories/:id(.:format)       categories#update 
          DELETE /categories/:id(.:format) 
+0

你有沒有想過兩次? 'category_articles'毫無疑問地顯示出,對於相應的幫助者,您需要提供一個類別而不需要文章。有了'categories_articles',你會感到困惑。 – jdoe 2012-04-21 20:28:09

+0

'資源:類別,:as =>'categories'do' – DanS 2012-04-21 20:29:27

+0

@DanS - 您的代碼無法按預期工作。 – Backo 2012-04-21 20:31:53

回答

0

如果你不滿意所提供的助手,那麼你可以使用魔法:

<%= link_to 'One', [:one, @category, Article] %> 
# /categories/123/articles/one 

<%= link_to 'Four', [:four, @category, @article] %> 
# /categories/123/articles/456/four 

<%= link_to 'Edit the article', [:edit, @category, @article] %> 
# /categories/123/articles/456/edit 

<%= link_to 'All articles for the category', [@category, Article] %> 
# /categories/123/articles 

非常相同的方法可以用來指定:url選項爲form_for幫手。

我希望你明白了!

P.S.請注意,使用類(如Article)指定您希望查看所有記錄並使用模型實例(如@article)來表示您即將看到一篇文章。