Search code examples
ruby-on-railsruby-on-rails-5cancan

Rails 5 add custom action to REST route with :id


In my Rails 5 app I have a route for a REST controller:

resources :payments

I want to add a new action pay to the resource, so I put:

resources :payments do
   get 'pay'
end

Where as my original routes look for example like this:

/payments/:id/edit

The custom route uses :payment_id instead of :id

/payments/:payment_id/pay

How can I change the route to:

payments/:id/pay

?

I need this because CanCanCan automatically sets the payment in the controller but looks for :id and not for :payment_id


Solution

  • resources :payments do
      get 'pay', on: :member
    end
    

    as described here.