我想设置一个用户为管理员,其方法是:Ruby on Rails的update_attibutes不工作
def set_user_admin
if admin_true? == true
user = User.find(params[:format])
if user == nil
redirect_to managements_path
else
user.update_attributes(admin: true, assistant: true,
businessman: true)
redirect_to managements_path
flash[:notice] = "The user #{user.name} is an admin user now"
end
else
end
end
的方法运行得很好,但在数据库中不节能。一些验证正在停止该操作。然后我在终端运行命令:
u = User.find_by_id(3)
u.update_attributes(admin: true)
(0.1ms) rollback transaction
=> false
u.errors
@messages={:password=>["Choose a password", "Your password must be at least 4 characters
"], :password_confirmation=>["You must confirm your password
"]}
所以,我不能更新用户作为管理员,因为在操作中调用密码验证。
有谁知道为什么在update_attributes中调用password和update_password验证?我不明白为什么
不,不,格式返回对象。当我打印时:flash [:notice] =“用户#{user.name}现在是管理员用户”我收到正确的名称。 –
可以将您的模型文件发布给用户验证的地方 – user3775217