资源和资源方法的区别。

时间:2021-01-14 11:00:02

What is the logical difference between resource and resources methods

资源和资源方法的逻辑区别是什么?

Here is some examples:

这里是一些例子:

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

It looks like method resource does not create route for index, and helpers in some cases are different (new_order and new_orders). Why?

看起来方法资源没有为索引创建路由,而且在某些情况下,helper是不同的(new_order和new_order)。为什么?

2 个解决方案

#1


40  

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

helper函数也应该有所不同,但不应该像示例中那样,因为约定是使用资源方法中的单数形式,使用资源方法的复数形式

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

And the logical difference is to declare you logically can't have the plural for resource in your app, for example Admin or whatever

逻辑上的区别是声明你的应用程序中不能有资源的复数,比如Admin之类的

#2


95  

At a high level, the intent of resource is to declare that only one of these resources will ever exist. For example:

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

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

As a user, I should only be able to update my own profile. I should never be able to edit other users' profiles, so there's no need for a URL scheme like /users/1/profile/edit. Instead, I use /profile/edit, and the controller knows to use the current user's ID rather than the ID passed in the URL (since there is none).

作为一个用户,我应该只能更新我自己的个人资料。我不应该能够编辑其他用户的配置文件,所以不需要像/users/1/profile/edit这样的URL模式。相反,我使用/profile/edit,控制器知道使用当前用户的ID而不是URL中传递的ID(因为没有)。

That's why you don't get an index action with resource: there's only one resource, so there's no sense in "listing" them.

这就是为什么没有使用resource的索引操作:只有一个资源,所以“列出”它们是没有意义的。

#1


40  

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

helper函数也应该有所不同,但不应该像示例中那样,因为约定是使用资源方法中的单数形式,使用资源方法的复数形式

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

And the logical difference is to declare you logically can't have the plural for resource in your app, for example Admin or whatever

逻辑上的区别是声明你的应用程序中不能有资源的复数,比如Admin之类的

#2


95  

At a high level, the intent of resource is to declare that only one of these resources will ever exist. For example:

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

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

As a user, I should only be able to update my own profile. I should never be able to edit other users' profiles, so there's no need for a URL scheme like /users/1/profile/edit. Instead, I use /profile/edit, and the controller knows to use the current user's ID rather than the ID passed in the URL (since there is none).

作为一个用户,我应该只能更新我自己的个人资料。我不应该能够编辑其他用户的配置文件,所以不需要像/users/1/profile/edit这样的URL模式。相反,我使用/profile/edit,控制器知道使用当前用户的ID而不是URL中传递的ID(因为没有)。

That's why you don't get an index action with resource: there's only one resource, so there's no sense in "listing" them.

这就是为什么没有使用resource的索引操作:只有一个资源,所以“列出”它们是没有意义的。