【问题标题】:Devise: Allow users to edit their account without providing a password BUT also use 'reconfirmable' functionality设计:允许用户在不提供密码的情况下编辑他们的帐户,但也使用“可重新确认”功能
【发布时间】:2014-07-01 14:32:28
【问题描述】:

我一直在 Devise wiki 中关注此操作指南...

How To: Allow users to edit their account without providing a password

...使我的用户无需提供现有密码即可更改帐户凭据和更新。

不过,我也想使用 Confirmable 模块 reconfirmable 功能

即使我在我的设计初始化程序文件中设置了config.reconfirmable = true,控制器似乎也没有发送可重新确认的电子邮件。

有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails devise ruby-on-rails-3.2 devise-confirmable


    【解决方案1】:

    问题在于原始 How-to 中的控制器缺少可确认模块所需的各种设计方法。

    为了让它工作,我必须尽可能多地使用Devise::RegistrationsController 中的原始update 操作,并覆盖update_resource(resource, params) 操作,这样它就不需要密码来更新记录。

    所以我的定制 RegistrationsController 现在看起来像这样:

    class Users::RegistrationsController < Devise::RegistrationsController
    
      def update
        update_params = account_update_params
        # required for settings form to submit when password is left blank
        if update_params[:password].blank?
          [:password,:password_confirmation,:current_password].collect{|p| update_params.delete(p) }
        end
        if update_params[:password].present? and update_params[:current_password].blank?
          [:current_password].collect{|p| update_params.delete(p) }
        end
    
        self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key)
        prev_unconfirmed_email = resource.unconfirmed_email if resource.respond_to?(:unconfirmed_email)
    
        if update_resource(resource, update_params)
          yield resource if block_given?
          if is_flashing_format?
            flash_key = update_needs_confirmation?(resource, prev_unconfirmed_email) ?
              :update_needs_confirmation : :updated
            set_flash_message :notice, flash_key
          end
          sign_in resource_name, resource, bypass: true
          respond_with resource, location: after_update_path_for(resource)
        else
          clean_up_passwords resource
          respond_with resource
        end
      end
    
      protected
    
      def update_resource(resource, params)
        resource.update_attributes(params)
      end
    end
    

    希望这对某人有所帮助!

    【讨论】:

    • 完美的解决方案,这个问题花了我这么长时间,但你的回答只是我对我的问题的解决方案 =) 非常感谢
    • @jellybean_232 谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-09
    • 1970-01-01
    相关资源
    最近更新 更多