gpt4 book ai didi

ruby-on-rails - 无需密码即可更新 `User` 属性

转载 作者:行者123 更新时间:2023-12-03 10:21:22 25 4
gpt4 key购买 nike

现在,用户无需输入密码就可以编辑他们的一些属性,因为我的验证是这样设置的:

validates :password, :presence =>true, :confirmation => true, :length => { :within => 6..40 }, :on => :create
validates :password, :confirmation => true, :length => { :within => 6..40 }, :on => :update, :unless => lambda{ |user| user.password.blank? }

但是,在用户执行此操作后,他们的密码将被删除 - update_attributes 正在将其密码更新为“”。这是我的更新定义:
def update

if @user.update_attributes(params[:user])
flash[:success] = "Edit Successful."
redirect_to @user
else
@title = "Edit user"
render 'edit'
end
end

我还尝试使用使用 update_attribute 的不同定义:
def save_ff
@user = User.find(params[:id])
@user.update_attribute(:course1, params[:user][:course1] )
@user.update_attribute(:course2, params[:user][:course2] )
@user.update_attribute(:course3, params[:user][:course3] )
@user.update_attribute(:course4, params[:user][:course4] )
redirect_to @user
end

但出于某种原因,这是在做同样的事情。如何在不更改密码的情况下更新某些用户属性?谢谢!

最佳答案

我没有意识到我昨天给你的解决方案会导致这个问题。对不起。

嗯,汲取灵感 from devise ,您应该简单地以这种方式更新您的 Controller :

def update
params[:user].delete(:password) if params[:user][:password].blank?
if @user.update_attributes(params[:user])
flash[:success] = "Edit Successful."
redirect_to @user
else
@title = "Edit user"
render 'edit'
end
end

关于ruby-on-rails - 无需密码即可更新 `User` 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7083575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com