资源和资源方法之间的区别 [英] Difference between resource and resources methods

查看:17
本文介绍了资源和资源方法之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

resourceresources 方法的逻辑区别是什么

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

看起来方法 resource 没有为 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?

推荐答案

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

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆