设计电子邮件和密码不能为空

时间:2014-04-07 11:10:44

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

我在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

Devise几天前工作正常我不知道搞砸了什么我已经添加了active_admin可能会产生冲突任何帮助吗?

2 个答案:

答案 0 :(得分: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)来明确允许更新您希望更新的属性,否则它们将不会传递给用户表进行更新。

答案 1 :(得分:0)

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

attr_accessible :email, :password, :role

这适用于gem“protected_attributes”