I have a controller in Ruby on Rails and I would like to do the following:
When a user is trying to access the controller I want to match some parameters. For example
domain.com/hello/19213/sayHello
I want to translate this to:
domain.com/hello/:id/:method
in my routes files.
Keep in mind that :method parameter might not exist but :id will always be there.
In addition if it is possible I would like to match the specific :method with a method in my controller. If not I plan to use a switch case.
Can someone provide an example for this and what I have to put in my routes.rb?
I found this:
match ':controller(/:action(/:id(.:format)))'
But I believe that this is somehow different as I only want to match router for the specific controller not every controller in my project.
Thanks
By method, I assume you mean a method you have in your model. If that is the case, I believe you want something like this in your routes.rb
The parentheses around :method mean that it is optional.
If you do not include a method, it will evaluate your SHOW action with params[:id] = :id
If you do include a method, it will evaluate the :method with params[:id] = :id
This is for methods you have defined in your controller