Ruby on Rails-资源和资源方法之间的区别

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

这是一些例子:

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)。 为什么?

alexkv asked 2020-06-27T05:36:18Z
2个解决方案
97 votes

在较高的层次上,index的目的是声明这些资源中只有一种会存在。 例如:

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

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

这就是为什么您不会在resource上执行index动作的原因:只有一种资源,因此“列出”它们没有意义。

Brandan answered 2020-06-27T05:37:12Z
43 votes

实际上,您是对的,除非您明确要求使用索引操作,否则resource不应创建索引操作:

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

辅助函数也应该有所不同,但与您的示例中的差别不应该太大,因为约定是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或其他任何形式

alony answered 2020-06-27T05:36:43Z
translate from https://stackoverflow.com:/questions/9194767/difference-between-resource-and-resources-methods