Difference between resource and resources methods

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

What is the logical difference between resource and resources methods

Here is some examples:

resource :orders, :only =>          


        
2条回答
  •  死守一世寂寞
    2020-12-01 00:05

    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

    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

提交回复
热议问题