【问题标题】:Using Strong Parameters With Devise在设计中使用强参数
【发布时间】:2014-08-15 22:10:45
【问题描述】:

我已使用 Devise 进行身份验证。

继续更新我的用户数据库以包含一个生物列。添加了这个并运行了迁移,所以我可以看到它在那里。

现在我希望用户能够在登录后添加个人简介。做了一些研究,我发现 attr_accessible 在 rails 4 中不再存在,我应该使用强参数。还检查了设计文档,但找不到我想要的。

我看到它们已添加到控制器中,但由于我使用了 Devise,我无权访问 UsersController

如何为用户添加更新 bio 字段以将其保存在数据库中的功能?

【问题讨论】:

标签: ruby-on-rails devise strong-parameters


【解决方案1】:

您应该在 ApplicationController 中添加一个 before_filter 来做到这一点。 Devise docs 包含解释这一点的部分。我从那里拿了下面的代码:

class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) << :username
  end
end

在例子中,属性 :username 被允许在 sign_up 页面中被解析。

【讨论】:

  • 谢谢 Victor 我错过了我应该看到的 account_update 部分,它解释了我需要什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多