【问题标题】:Devise email and password can not be blank设计邮箱和密码不能为空
【发布时间】:2014-04-07 11:10:44
【问题描述】:

我的 application_controller.rb 中有这段代码

before_filter :update_sanitized_params, if: :devise_controller?
before_filter :store_location
protect_from_forgery with: :exception
def update_sanitized_params
    devise_parameter_sanitizer.for(:sign_up) {|u| u.permit(:email, :password, :password_confirmation, :role)}
    devise_parameter_sanitizer.for(:sign_in) {|u| u.permit(:email, :password) }
end

但我仍然有来自设计的验证错误

Email can't be blank
Password can't be blank
Password can't be blank
Role is not included in the list

几天前设计工作正常我不知道它出了什么问题我添加了active_admin可能是这造成了冲突任何帮助??

【问题讨论】:

    标签: ruby-on-rails-4 devise activeadmin


    【解决方案1】:

    假设您在更新用户记录时遇到验证错误。

    def update_sanitized_params
        devise_parameter_sanitizer.for(:sign_up) {|u| u.permit(:email, :password, :password_confirmation, :role)}
        devise_parameter_sanitizer.for(:sign_in) {|u| u.permit(:email, :password) }
        ## Permit the attributes for account_update
        devise_parameter_sanitizer.for(:account_update) {|u| u.permit(:email, :password, :password_confirmation, :role) }
    end
    

    您需要通过指定devise_parameter_sanitizer.for(:account_update) 明确允许您希望更新的属性,否则它们不会被传递到用户表进行更新。

    【讨论】:

    • 是的,我只是在 user.rb 中添加 attr_accessible :email, :password, :role ,一切都会好起来的。谢谢
    【解决方案2】:

    我已经通过在 user.rb 中添加这一行来解决问题

    attr_accessible :email, :password, :role
    

    这适用于 gem "protected_attributes"

    【讨论】:

      猜你喜欢
      • 2014-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-17
      • 2013-12-06
      相关资源
      最近更新 更多