【问题标题】:Rails 4.0.0, Device 3.0.0.rc :: can't mass-assign protected attributesRails 4.0.0,设备 3.0.0.rc :: 不能批量分配受保护的属性
【发布时间】:2013-07-04 02:05:39
【问题描述】:

从基于 Ruby 1.9.3 的 Rails 3.2.13 升级到 Rails 4.0.0。

Devise 3.0.0.rc 不会批量分配。我遵循了我发现的所有建议,从这里描述的两种方法开始:https://github.com/plataformatec/devise#strong-parameters

登录/注销有效,但注册无效:

Started POST "/users" for 127.0.0.1 at 2013-07-03 21:01:47 -0500
Processing by RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", 
"authenticity_token"=>"5QPwkbXULEsF5nCiMMK7zd04baZ5kOecgqV2BdRnSEc=", 
"user"=>{"first_name"=>"test", "last_name"=>"testor", "email"=>"asdhd@gmail.com",
"username"=>"testy", "password"=>"[FILTERED]", 
"password_confirmation"=>"[FILTERED]"}, "commit"=>"Register"}
WARNING: Can't mass-assign protected attributes for User: email, password, 
password_confirmation, first_name, last_name, username

这是 Devise 的问题吗?有人有解决办法吗?

谢谢!

【问题讨论】:

  • 也许你在你的应用程序中覆盖了 devise/registrations_controller?
  • 还要验证您是否从 User 模型中删除了 attr_accessible
  • 是的,一切都做了,我想出了问题所在,请看下面的答案。
  • 很高兴听到您解决问题

标签: devise ruby-on-rails-4


【解决方案1】:

问题是我在 railscasts http://railscasts.com/episodes/415-upgrading-to-rails-4 之后使用的 gem 'protected_attributes':

...
# add these gems to help with the transition:
gem 'protected_attributes'
...

我没有想到 gem 会阻碍 Rails 4 方法,例如 params.require(:incoming_model).permit( .... )。我可能误会了。

取出宝石可以解决问题。这里提到的第一种方法(懒惰的方法)有效:https://github.com/plataformatec/devise#strong-parameters

【讨论】:

    【解决方案2】:

    在您的 application.rb 中将 config.active_record.whitelist_attributes 设置为 false。

    include ActiveModel::ForbiddenAttributesProtection 添加到您的模型中

    看看:http://blog.remarkablelabs.com/2012/12/strong-parameters-rails-4-countdown-to-2013

    【讨论】:

    • config.active_record.whitelist_attributes 在您的 application.rb 中为 false。在 R4 中不再是一个选项。
    • 并且 strong_parameters gem 是 R4 的一部分。但是我还是试了一下,结果一样,不行。
    猜你喜欢
    • 2023-03-25
    • 1970-01-01
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    相关资源
    最近更新 更多