升级到Rails 4时,批量分配失败

jua*_*tas 6 ruby-on-rails attr-accessible ruby-on-rails-3 strong-parameters ruby-on-rails-4

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)
Run Code Online (Sandbox Code Playgroud)

我尝试了消息所说的内容,并添加gem 'strong_parameters'到我的内容中Gemfile.

但是当我这样做时,rails s我得到了上面的错误.

更新

我试过了:

config.active_record.whitelist_attributes = true
Run Code Online (Sandbox Code Playgroud)

在confgi/application.rb,也与false,但实际上我不明白这个选项.

Chr*_*itz 6

attr_accessible并attr_protected已被拉出Rails 4并提取到protected_attributes.将其捆绑到您的应用程序中,然后您应该能够再次使用它们.

话虽如此,建议你使用strong_parameters而不是attr_accessible这些天,所以最终你会想要迁移到那个.


Tom*_*gen 4

在您的 Gemfile 中,您会注意到它gem 'protected_attributes'已被散列出来。删除哈希值。跑步bundle install。

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

有关 Strong_parameters 的更多信息,请参阅此链接。