Ayush Mishra
Ayush Mishra

Reputation: 506

Give argument to action on Rails

How do I give an argument to my action of a controller.

I have an action named action and a controller named controller. I want to give an argument to the action when I go to

http://localhost:3000/controller/action/argument1 or to

http://localhost:3000/controller/action/argument2

How do I know, which argument was actually passed in my action?

Upvotes: 0

Views: 52

Answers (1)

joshua.paling
joshua.paling

Reputation: 13952

The documentation is the best place to start. Right up the very top, there's an example of passing in a parameter (in this case, id):

get '/patients/:id', to: 'patients#show'

(the line above would go in your routes.rb file). The id param would then be available in your controller, in params[:id]

So in your case, you'd have a line in your routes.rb with something like:

get '/controller/action/:argument', to: 'controller#action'

And then you could refer to your argument in your controller as params[:argument]

Upvotes: 2

Related Questions