【问题标题】:Rails 4.1 route path helper using incorrect id in path generationRails 4.1路由路径助手在路径生成中使用不正确的ID
【发布时间】:2018-10-01 16:39:56
【问题描述】:

我正在从 Rails 4.0.13 升级到 Rails 4.1.16... 正在升级到 Rails 5。

@account和@facility是ActiveRecord模型实例,在控制器FacilitiesController中检索成功

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

routes.rb 看起来像这样:

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

请求期间在 Rails 4.1.16 控制台中:

> 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??

现在生成的路径中的设施 ID 不同。应该是 2035637533,就像在最后一个请求之前的 _path 请求中一样。

谁能帮我理解_path helper 的行为差异,因为第三个参数?

在 Rails 4.0 中,我没有注意到这种行为。路径按预期生成,路径中的设施 ID 设置为 2035637533。

【问题讨论】:

  • 在你做最后一条路线之前,设施的id还是一样吗?
  • 是的。 @facility 保持不变。

标签: ruby-on-rails ruby-on-rails-4.1


【解决方案1】:

我的猜测是您正在合并参数,即合并 id 键并将 id 设置为帐户 id。试试:

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

【讨论】:

  • 所以...这实际上解决了问题。万岁。我想在任何情况下,在路径助手中传递params 的任何衍生物都是一个坏主意。
猜你喜欢
  • 2013-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多