ale*_*xkv 46 routes ruby-on-rails ruby-on-rails-3
resource和resources方法之间的逻辑区别是什么
以下是一些例子:
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
Run Code Online (Sandbox Code Playgroud)
看起来方法resource不会创建路径index,并且在某些情况下帮助程序是不同的(new_order和new_orders).为什么?
Bra*_*dan 97
在较高的层面上,目的resource是宣布这些资源中只有一个将存在.例如:
resource :profile, :only => [:edit, :update]
Run Code Online (Sandbox Code Playgroud)
作为用户,我应该只能更新自己的个人资料.我永远不能编辑其他用户的个人资料,因此不需要像以下那样的URL方案/users/1/profile/edit.相反,我使用/profile/edit,并且控制器知道使用当前用户的ID而不是URL中传递的ID(因为没有).
这就是为什么你没有得到一个index动作resource:只有一个资源,所以"列出"它们是没有意义的.
alo*_*ony 43
实际上你是对的,resource不应该创建索引操作,除非你明确要求索引操作,这样:
resource :orders, :only => [:index, :create, :show]
Run Code Online (Sandbox Code Playgroud)
助手也应该有所不同,但不如你的例子那么多,因为惯例是使用单一形式的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
Run Code Online (Sandbox Code Playgroud)
逻辑上的区别在于声明您在应用程序中逻辑上不能拥有复数资源,例如Admin或其他
| 归档时间: |
|
| 查看次数: |
19417 次 |
| 最近记录: |