【问题标题】:How to use plural names for nested resources?如何为嵌套资源使用复数名称?
【发布时间】:2012-04-21 20:21:06
【问题描述】:

我正在使用 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/categories_articles代替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)  

【问题讨论】:

  • 您是否三思而后行? category_articles 毫无疑问地表明,对于相应的助手,您需要提供一个类别而不是文章。使用categories_articles,您会感到困惑。
  • resources :categories, :as => 'categories' do
  • @DanS - 您的代码没有按预期工作。
  • @jdoe - 我想使用这种方法,因为我想成功使用例如polymorphic_path 方法。此时如果我使用polymorphic_path(Categories::Article),我会收到NoMethodError 错误,因为它返回categories_articles_path(而不是category_articles_path)。

标签: ruby-on-rails ruby-on-rails-3 routing routes


【解决方案1】:

如果您对提供的帮助程序不满意,那么您可以使用魔法:

<%= 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

同样的方法可用于为form_for 助手指定:url 选项。

我希望你明白了!

附:请注意使用类(如Article)表示您想查看所有记录,并使用模型实例(如@article)表示您将要看到一篇文章。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-29
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多