我正在开发一个使用Rails和Devise进行用户身份验证的应用程序。我想知道是否有办法只为了一些更改而要求密码。设计:仅在某些情况下要求密码
举例来说,我想询问密码时:
- 删除帐户
- 更改密码
而且我希望用户可以自由编辑等领域,而不任何密码,如:
- 名称
- 用户名
- 阿凡达
所以,我想某种程度上,这两种方法之间切换。我该如何解决这个问题?
编辑:
在设计的文档,我发现这一点,它工作得很好,只允许更改密码,电子邮件,如果我输入密码:
def update
@user = User.find(current_user.id)
successfully_updated = if needs_password?(@user, params)
@user.update_with_password(params[:user])
else
# remove the virtual current_password attribute update_without_password
# doesn't know how to ignore it
params[:user].delete(:current_password)
@user.update_without_password(params[:user])
end
if successfully_updated
set_flash_message :notice, :updated
# Sign in the user bypassing validation in case his password changed
sign_in @user, :bypass => true
redirect_to after_update_path_for(@user)
else
render "edit"
end
end
private
# check if we need password to update user data
# ie if password or email was changed
# extend this as needed
def needs_password?(user, params)
user.email != params[:user][:email] ||
!params[:user][:password].blank?
#HERE
end
现在,我能放什么#HERE
到当我删除帐户时还需要密码?
您不能添加任何'#HERE',因为'update'方法不会删除用户,它只会更新用户属性。你可能想要添加相同类型的东西到你的控制器 – ted 2013-04-07 20:51:25