How to add a custom route to a singleton resource?

map.resource  :basket, :collection => { :checkout => :post }

The above does not work for the resource, as you would expect, since the basket is a resource (i.e. a singular), not a resource, so there is no concept of a collection, everything should be tied to current_user. In this case, the user has_one Basket.

However, I would like to specify a custom route without resorting to adding another line to the routes, for example:

map.checkout 'basket/checkout', :controller => 'baskets', :action => 'checkout'

Is it possible?

Of course, my other option is to add a check controller.

+3
source share
2 answers

Just use :memberinstead :collection:

map.resource :basket, :member => {:checkout => :post}
0
source

If the basket is tied to the user, I would make it a sub-resource:

map.resources :users do |users|
  users.resource :basket, :member => { :checkout => :post }
end

... or in Rails 3 ...

resources :users do
  resource :basket do
    post :checkout, :on => :member
  end
end

, , . URL- :

/users/5/basket/checkout

"checkout_user_basket".

0

Source: https://habr.com/ru/post/1761951/


All Articles