redirect_to accepts 4 types of inputs:
Hash, Record, String with protocol, String without protocol and Back
I'm trying to redirect from a controller to a nested resource.
The nested resource is specified in the routes as followed:
map.resources :employees do |employee|
employee.resources :addresses
end
Redirecting to this is simple by using the string of redirect_to
redirect_to address_path(employee)
But I want to pass a parameter with this redirect.
Unfortunately this is impossible as redirect_to with a string does not handle any options.
We have to use the hash options for that. So, I tried that:
redirect_to :controller => 'addresses', :action => 'index', :pass_param = 1, :employee_id = employee.id
This however (as expected) lands you in /addresses?pass_param=1&employee_id=1, not in /employees/1/addresses?pass_param=1.
The workaround I used was:
redirect_to address_path(employee_id) + '?pass_param=1'
Which I think is really ugly, especially when I want to use more parameters.
There are two solutions to this: either be able to specify the route to your nested resource as an option in the redirect_to hash OR accept an options hash for redirect_to string and add the parameters that way.