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

soのステートメントを変更して、「部分」に対してのみ複数の名前config/routes.rbを持つ次のルーターを生成したいと思います(つまり、 / の代わりに / をそれぞれ使用したいと思います) 。category_articlecategories_articlecategories_articlescategory_articlecategory_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)  
4

1 に答える 1

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

あなたがアイデアを得たことを願っています!

PS クラス ( などArticle) を使用してすべてのレコードを表示することを指定し、モデル インスタンス ( など@article) を使用して 1 つの記事を表示しようとしていることに注意してください。

于 2012-04-22T06:42:10.333 に答える