如何从设计表单中分离更改密码 [英] How to separate change password from devise form

查看:218
本文介绍了如何从设计表单中分离更改密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



1)更改默认的编辑用户表单 - devise提供 - 删除密码,并允许其他字段进行更新而不必输入密码,即删除默认的密码验证。

2)创建一个用于更改密码的单独表单



我有一切工作,只有一个问题,在更新密码的单独表格中,我包含了一个当前密码的字段。当使用表单时,不验证当前密码,所以我改变了

  @ user.update_attributes(params [:user] )

  @ 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 @user,:bypass => true
redirect_to after_update_path_for(@user)
else
clean_up_passwords(resource)
respond_with_navigational(resource)do
if params [:change_password]#或flash [:change_password]
render:change_password
else
render:edit
end
end
end
end

谢谢!

解决方案1 ​​



我发现问题的解决方案(尽管非常混乱) p>

  def update 
@user = User.find(current_user.id)

if params [ :用户] [:密码] .blank?
if @ user.update_attributes(params [:user])
set_flash_message:notice,:updated
#当用户密码改变时,绕过验证
sign_in @user, :bypass => true
redirect_to after_update_path_for(@user)
else
respond_with_navigational(resource)do
render:edit
end
end
else
如果@ user.update_with_password(params [:user])
set_flash_message:notice,:updated
#如果用户密码改变,则绕过验证
sign_in @user,:bypass = > true
redirect_to after_update_path_for(@user)
else
clean_up_passwords(资源)
respond_with_navigational(资源)do
render:change_password
end
end
end



解决方案2



你能提出一个更好的解决方案吗?

解决方案

这两种情况都有例子


  1. https://github.com/plataformatec/devise/wiki/How-To:-Allow-用户编辑他们的帐户不提供密码

  2. https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit -their-password

您应该看看 @ user.update_with_password(params [:用户]) vs @ user.update_attributes(params [:user])


I am trying to do two things:

1) Change the default "edit user form" - provided with devise - to remove "password" and allow the other fields to be updated without having to enter a password ie remove the default validation for password.

2) Create a separate form for changing password

I have got everything to work, there is only one problem, in the separate form for updating password, I have included a field for current password. When using the form, no validation is made for current password, so I changed

@user.update_attributes(params[:user]) 

to

@user.update_with_password(params[:user])

This worked, however it raised another issue. Back in the main form with all the other details except password, the form now asks for a "current password". How can I achieve this without a validation for current password being called on the main form?

here is my registrations controller:

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

Thanks!

Solution 1

I have found a solution to the problem (albeit a very messy one):

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

Solution 2

Can you suggest a better solution?

解决方案

Did you bother to check out Devise wiki? There are examples for both this cases

  1. https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-account-without-providing-a-password
  2. https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-password

You should be looking at @user.update_with_password(params[:user]) vs @user.update_attributes(params[:user])

这篇关于如何从设计表单中分离更改密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆