0

my Rake Routes looks like this:

report GET /management/:id/report(.:format) report#show

In my controller code how can I access that :id parameter? Is it still by .params[:id] ?

I think I am confused because this time id is not the last thing in a URL and it is in the middle of the URL.

2 Answers 2

1

Yes you can acces to the params you defined in your routes like you said:

# The route 
# /management/:id/report(.:format)
# will generate the following params:
params[:id]
params[:format] # optional

Another example:

match ':controller(/:action(/:id))'
# will produce the following params:
params[:controller]
params[:action] # (optional)
params[:id] # (optional)

match '/search/:search'
# will produce, in the SearchController (and views):
params[:search]
Sign up to request clarification or add additional context in comments.

2 Comments

so it is working by the "name" of the params? if I was naming it "provider_id" I could call that in the code? that's cool. Did I understand it correctly?
Yes you do understand it correctly @user1899082! You can name your params as you want ;)
1

It is params[:id]. It's just a named parameter that doesn't have to be the last part of the url. If it doesn't work please provide your routes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.