Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8462 (closed defect: invalid)

Opened 2 years ago

Last modified 2 years ago

redirect_to does not pass parameters when using controller_path

Reported by: BCC Assigned to: core
Priority: normal Milestone:
Component: ActionPack Version: edge
Severity: normal Keywords: redirect_to hash string options controller_path
Cc:

Description

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.

Change History

05/25/07 16:15:32 changed by josh

  • status changed from new to closed.
  • resolution set to invalid.
redirect_to employee_address_path(: employee_id => employee_id, :pass_param => 1)

05/29/07 07:50:51 changed by BCC

You might want to put this in the redirect_to documentation :)