【问题标题】:Mass assignment fails when upgrading to Rails 4升级到 Rails 4 时质量分配失败
【发布时间】:2013-07-29 03:49:15
【问题描述】:
deprecated_mass_assignment_security.rb:17:in `attr_accessible': `attr_accessible` is extracted out of Rails into a gem. Please use new recommended protection model for params(strong_parameters) or add `protected_attributes` to your Gemfile to use old one. (RuntimeError)

我尝试了消息中的内容,将gem 'strong_parameters' 添加到我的Gemfile

但是当我执行rails s 时,我得到了上面的错误。

更新

我试过了:

config.active_record.whitelist_attributes = true

confgi/application.rb,还有false,但实际上我不明白那个选项。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 strong-parameters attr-accessible


    【解决方案1】:

    attr_accessibleattr_protected 已从 Rails 4 中提取出来并提取到 protected_attributes 中。将其捆绑到您的应用中,然后您应该可以再次使用它们。

    话虽如此,现在建议您使用strong_parameters 而不是attr_accessible,所以最终您会想要迁移到那个位置。

    【讨论】:

    • 谢谢,如何将它捆绑到我的应用程序中?我的 Gemfile 中有 protected_attributes。
    • 只需从命令行运行 bundle install 就可以了!
    • 您是否删除了strong_parameters 并再次将whitelist_attributes 设置为true?您应该只需要包含它,如果您的应用程序使用 Rails 3,您应该拥有批量分配保护所需的一切。
    • 自从我在 Rails 3 中,我在任何地方都没有强大的参数,它们是否在 Rails 3 应用程序的某个地方?我试过whitelist_attributes 真假,同样的问题。
    • 如果您仍在运行 Rails 3,则不需要添加受保护的属性 gem,因为这是 Rails 3 的一部分。如果您尝试将应用程序迁移到Rails 4。强参数只是 Rails 4 的一部分,所以除非它被捆绑在你的应用程序中,否则你不应该有它。
    【解决方案2】:

    在您的 Gemfile 中,您会注意到 gem 'protected_attributes' 已被散列。 删除哈希。 运行bundle install

    但由于 protected_attributes 已被弃用,并且将来可能会消失,请使用上述帖子中提到的 strong_parameters。

    有关 strong_parameters 的更多信息,请参阅link

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-20
      • 2014-08-12
      • 2014-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多