ruby-on-railsruby-on-rails-4.1

Rails 4.1 route path helper using incorrect id in path generation


I'm working on an upgrade from Rails 4.0.13 to Rails 4.1.16... en route to Rails 5.

@account and @facility are ActiveRecord model instances, retrieved successfully in the controller FacilitiesController

@account = #<Account id: 1863133034>
@facility = #<Facility id: 2035637533>

routes.rb looks like this:

resources :accounts do
  resources :facilities do
    get :report
  end
end

In Rails 4.1.16 console during the request:

> request.env["REQUEST_PATH"]
=> "/accounts/1863133034/facilities/2035637533"

> params
{"action"=>"show", "controller"=>"facilities", "account_id"=>"1863133034", "id"=>"2035637533"}

> account_facility_report_path( @account, @facility, {})
=> "/accounts/1863133034/facilities/2035637533/report"


> account_facility_report_path( @account, @facility, params.merge( format: :pdf ) )
=> "/accounts/1863133034/facilities/1863133034/report.pdf?id=2035637533"  # why different facility ID??

The facility ID is different now in the generated path. Should be 2035637533, like in the _path request just preceding this last one.

Can someone help me understand the difference in behaviour of the _path helper because of the 3rd parameter?

In Rails 4.0, I do not notice this behaviour. The path is generated as expected, with facility ID in the path set to 2035637533.


Solution

  • My guess is that you are merging params, which is merging the id key and setting id to the account id. Try:

    account_facility_report_path( @account, @facility, format: :pdf )