ruby-on-rails - 如何将更改密码与设计表单分开

标签 ruby-on-rails forms devise change-password

我正在尝试做两件事:

1) 更改默认的“编辑用户表单” - 随设计提供 - 以删除“密码”并允许更新其他字段而无需输入密码,即删除密码的默认验证。

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

我已经完成了所有工作,只有一个问题,在用于更新密码的单独表单中,我包含了一个当前密码字段。使用表单时,没有对当前密码进行验证,所以我改变了

@user.update_attributes(params[:user]) 


@user.update_with_password(params[:user])

这奏效了,但它提出了另一个问题。回到带有除密码之外的所有其他详细信息的主表单,该表单现在要求输入“当前密码”。如何在不验证主表单上调用的当前密码的情况下实现这一点?

这是我的注册 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

谢谢!

解决方案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

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

最佳答案

您是否费心查看 Devise wiki?这两种情况都有例子

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

  • 你应该看看 @user.update_with_password(params[:user])对比 @user.update_attributes(params[:user])

    关于ruby-on-rails - 如何将更改密码与设计表单分开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17584887/

    相关文章:

    ruby-on-rails - 设计邀请不验证模型

    ruby-on-rails - Devise Bootstrap Views 渲染错误 : couldn't find file 'devise_bootstrap_views' with type 'text/css'

    ruby-on-rails - 使用轮胎 gem 进行 Elasticsearch 的刻面尺寸

    图像字段未更新的 Django 内联表单集

    Angular 6 : How to hide radio circle using Angular Material and use NgStyle for checked answer?

    get - 为什么在 GET FORMS 的操作 URL 上丢失查询参数是这样设计的?

    ruby-on-rails - 如何覆盖 lib/spree/search/base.rb

    ruby-on-rails - friendly_id gem with rails,转换为 SEO 友好的 url

    css - Rails - 在 Rails 4 应用程序的供应商 css 中使用供应商字体 - Assets 管道

    ruby-on-rails - 设计如何覆盖 send_confirmation_instructions