Difference between resource and resources methods

后端 未结 2 1156
傲寒
傲寒 2020-11-30 23:50

What is the logical difference between resource and resources methods

Here is some examples:

resource :orders, :only =>          


        
2条回答
  •  生来不讨喜
    2020-11-30 23:54

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

    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.

提交回复
热议问题