我正在尝试完成两件事情:
1)更改Devise提供的默认“编辑用户表单”,删除“密码”并允许更新其他字段而无需输入密码,即删除密码的默认验证。
2)创建一个单独的表单来更改密码。
我已经使所有内容正常工作,只有一个问题,就是在用于更新密码的单独表单中,我包含了一个当前密码字段。但使用该表单时,没有对当前密码进行验证,因此我进行了更改。
@user.update_attributes(params[:user])
to
@user.update_with_password(params[:user])
这个方法可行,但引发了另一个问题。在主表单中,除密码外还有其他详细信息,现在该表单要求输入“当前密码”。如何实现这一点,而不需要在主表单上调用验证当前密码的函数呢?
以下是我的注册控制器:
def update
@user = User.find(current_user.id)
if @user.update_attributes(params[:user])
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
clean_up_passwords(resource)
respond_with_navigational(resource) do
if params[:change_password] # or flash[:change_password]
render :change_password
else
render :edit
end
end
end
end
谢谢!
解决方案1
我已经找到了一个解决问题的方案(尽管很混乱):
def update
@user = User.find(current_user.id)
if params[:user][:password].blank?
if @user.update_attributes(params[:user])
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
respond_with_navigational(resource) do
render :edit
end
end
else
if @user.update_with_password(params[:user])
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
clean_up_passwords(resource)
respond_with_navigational(resource) do
render :change_password
end
end
end
解决方案 2
你能提出更好的解决方案吗?