资源与资源方法之间的区别 [英] Difference between resource and resources methods
问题描述
资源
和资源
方法之间的逻辑区别是什么
What is the logical difference between resource
and resources
methods
以下是一些示例:
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
方法资源
似乎没有为 index
创建路线,在某些情况下辅助方法是不同(new_order和new_orders)。为什么?
It looks like method resource
does not create route for index
, and helpers in some cases are different (new_order and new_orders). Why?
推荐答案
实际上您是对的,资源
不应创建索引操作,除非您明确要求索引操作,否则采用以下方式:
Actually you are right, resource
should not create an index action, unless you ask for the index action explicitly, this way:
resource :orders, :only => [:index, :create, :show]
Helpers也应该有所不同,但与您的示例,因为约定是使用带有 resource
方法的单数形式,以及带有 resources
<的复数形式/ p>
Helpers should differ too, but not that much as in your example, because the convention is to use a singular form with the resource
method, and the plural with the 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或其他任何形式
And the logical difference is to declare you logically can't have the plural for resource in your app, for example Admin or whatever
这篇关于资源与资源方法之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!