【问题标题】:Difference between resource and resources methods资源和资源方法之间的区别
【发布时间】:2012-02-08 14:00:19
【问题描述】:

resourceresources 方法之间的逻辑区别是什么

这里有一些例子:

resource :orders, :only => [:index, :create, :show]

> rake routes
 orders POST       /orders(.:format)            orders#create
        GET        /orders(.:format)            orders#show


resources :orders, :only => [:index, :create, :show]

> rake routes
 orders GET        /orders(.:format)            orders#index
        POST       /orders(.:format)            orders#create
  order GET        /orders/:id(.:format)        orders#show


resource :orders

> rake routes
     orders POST       /orders(.:format)            orders#create
 new_orders GET        /orders/new(.:format)        orders#new
edit_orders GET        /orders/edit(.:format)       orders#edit
            GET        /orders(.:format)            orders#show
            PUT        /orders(.:format)            orders#update
            DELETE     /orders(.:format)            orders#destroy


resources :orders

> rake routes
     orders GET        /orders(.:format)            orders#index
            POST       /orders(.:format)            orders#create
  new_order GET        /orders/new(.:format)        orders#new
 edit_order GET        /orders/:id/edit(.:format)   orders#edit
      order GET        /orders/:id(.:format)        orders#show
            PUT        /orders/:id(.:format)        orders#update
            DELETE     /orders/:id(.:format)        orders#destroy

看起来resource 方法没有为index 创建路由,并且在某些情况下助手是不同的(new_order 和 new_orders)。为什么?

【问题讨论】:

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


    【解决方案1】:

    在较高级别上,resource 的意图是声明这些资源中只有一个将永远存在。例如:

    resource :profile, :only => [:edit, :update]
    

    作为用户,我应该只能更新自己的个人资料。我应该永远无法编辑其他用户的个人资料,因此不需要像/users/1/profile/edit 这样的 URL 方案。相反,我使用/profile/edit,并且控制器知道使用当前用户的 ID 而不是 URL 中传递的 ID(因为没有)。

    这就是为什么您没有通过resource 获得index 操作:只有一个资源,因此“列出”它们没有意义。

    【讨论】:

    • 有趣!感谢您的启发性解释。所以这意味着我们有与用户控制器不同的配置文件控制器?另外,如果我希望 url 看起来像 /profile/settings,我可以使用 collection { get 'settings' }
    【解决方案2】:

    其实你是对的,resource 不应该创建索引操作,除非你明确要求索引操作,这样:

    resource :orders, :only => [:index, :create, :show]
    

    Helpers 也应该有所不同,但不像您的示例那样大,因为惯例是使用单数形式和 resource 方法,而复数形式和 resources

    resources :orders
    => rake routes
    
         orders GET        /orders(.:format)            orders#index
                POST       /orders(.:format)            orders#create
      new_order GET        /orders/new(.:format)        orders#new
     edit_order GET        /orders/:id/edit(.:format)   orders#edit
          order GET        /orders/:id(.:format)        orders#show
                PUT        /orders/:id(.:format)        orders#update
                DELETE     /orders/:id(.:format)        orders#destroy
    
    resource :order
    => rake routes
          order POST       /order(.:format)            orders#create
      new_order GET        /order/new(.:format)        orders#new
     edit_order GET        /order/:id/edit(.:format)   orders#edit
                GET        /order/:id(.:format)        orders#show
                PUT        /order/:id(.:format)        orders#update
                DELETE     /order/:id(.:format)        orders#destroy
    

    逻辑上的区别是声明你的应用程序中的资源在逻辑上不能有复数形式,例如 Admin 或其他

    【讨论】:

    • 我想你的意思是第一行说resource :orders, :only ...
    • “助手也应该不同”:这意味着你得到new_order_path 对应resourcesnew_orders_path 对应resource
    • 主要区别不仅仅是索引页面。 -- resources 将创建带有索引页面的路由,并且还需要 :id 参数来编辑、更新、销毁和显示操作 ([Check]guides.rubyonrails.org/…) -- resource将创建没有索引路由且不需要 :id 参数的相同路由。 ([检查]guides.rubyonrails.org/routing.html#singular-resources)
    猜你喜欢
    • 1970-01-01
    • 2015-11-08
    • 1970-01-01
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多