Home > Back-end >  How to declare a dynamic routes scope/namespace variable in Rails?
How to declare a dynamic routes scope/namespace variable in Rails?

Time:11-11

I'd like to use the dynamic current_cart.name variable as scope or namespace in rails routes, for example:

scope current_car.name do
  get 'checkout'
  get 'toggle'
end

to where the name of the current_cart query is displayed the next form(depending of current car):

    ferrari/checkout
    ferrari/toggle
    mazda/checkout
    mazda/toggle
    ...
    fiat/checkout
    fiat/toggle

I have tried different ways with no success. I would really appreciate your time if you help me get the issue resolved.

Thanks and happy day for you.

CodePudding user response:

If you want to allow any car name in the URL then you can use scope but you'll need to tell which controller this route is going to use;

# `:car_name` can be replaced with anything else
# `:cars` just as an example controller
scope ':car_name/', controller: :cars do
  get 'checkout'
  get 'toggle'
end

It creates two routes like;

checkout GET   /:car_name/checkout(.:format)      cars#checkout
toggle   GET   /:car_name/toggle(.:format)        cars#toggle
  • Related