【问题标题】:Why are attributes coming as a string of a ActionController::Parameters object in Rails 5?为什么属性在 Rails 5 中作为 ActionController::Parameters 对象的字符串出现?
【发布时间】:2016-12-02 19:19:36
【问题描述】:

我刚刚升级到 Rails 5。

在为@shift 提交表单后,我尝试调用我的控制器:

@position = Position.find params[:shift][:position_id]

我收到“没有将符号隐式转换为整数”错误。

当我检查参数时:

<ActionController::Parameters {"shift"=>"#<ActionController::Parameters:0x007fecd4e83208>", "sms"=>"true", "controller"=>"admin/overtime", "action"=>"confirm"} permitted: true>

如果我打电话给:params.to_h,我会得到:

{"shift"=>"#<ActionController::Parameters:0x007fecd4e83208>", "sms"=>"true", "controller"=>"admin/overtime", "action"=>"confirm"}

shift 参数是"#&lt;ActionController::Parameters:0x007fecd4e83208&gt;"...一个字符串!?

如果我这样做:

params.require(:shift).permit(:position_id)

我明白了:

NoMethodError: undefined method `permit' for "#<ActionController::Parameters:0x007fecd4e83208>":String

如何检索这些值?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-5


    【解决方案1】:

    发生的事情是,在我的 ShiftsController 中,我重定向到 OvertimeController 确认操作:

    admin_overtime_confirm_path(shift: params[:shift], sms: true)
    

    由于在 Rails 5 中 ActionController::Parameters 不再继承自 HashWithIndifferentAccess,因此在应用程序中调用 params 现在将返回一个对象而不是哈希。所以它发送的是 ActionController::Parameters 对象的字符串版本。

    为了解决这个问题,我做了:

    admin_overtime_confirm_path(shift: params[:shift].permit!.to_h, sms: true)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-02
      • 2019-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-16
      相关资源
      最近更新 更多