【问题标题】:Rails 4 - Devise - Confirmable subject to approvalRails 4 - 设计 - 经批准可确认
【发布时间】:2016-05-18 02:12:15
【问题描述】:

我正在努力学习如何阅读 ruby​​docs。我敢肯定,在尝试使用这些文档时,我错过了一些基本的基本知识,但现在,我很沮丧,信息被呈现给我,但没有任何形式的易懂的用户指南与之配套.

我想修改我的设计,以便用户可以指定他们想要加入的组 - 在这种情况下,他们的电子邮件已更改为组织电子邮件的组织,等待组织所有者的确认。

我认为这个函数(我在下面从设计 ruby​​ 文档中复制)会有所帮助。

#postpone_email_change_until_confirmation_and_regenerate_confirmation_token ⇒ Object (protected)

253
# File 'lib/devise/models/confirmable.rb', line 247

def postpone_email_change_until_confirmation_and_regenerate_confirmation_token
  @reconfirmation_required = true
  self.unconfirmed_email = self.email
  self.email = self.email_was
  self.confirmation_token = nil
  generate_confirmation_token
end

我的问题是我不明白它是如何工作的或如何将它整合到我的应用程序中。

我是否将其添加到我的用户模型中(只需复制和粘贴),然后在我的用户控制器中添加一行以进行更新操作,以便电子邮件的任何更改都会执行方法中列出的每个步骤?

如果我这样做,我应该给控制器调用起什么名字?

在 user.rb 我试过了:

 def postpone_email_change_until_confirmation_and_regenerate_confirmation_token
      @reconfirmation_required = true
      self.unconfirmed_email = self.email
      self.email = self.email_was
      self.confirmation_token = nil
      generate_confirmation_token
    end

在用户控制器 - 更新操作中,我尝试过:

if @user.update(user_params)
user.postpone_email_change_until_confirmation_and_regenerate_confirmation_token

我显然试图错误地执行此操作。我不清楚我应该如何使用这个功能。

谁能看出我哪里出错了?

【问题讨论】:

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


    【解决方案1】:

    该方法受到Confirmable 关注。 因此,您只需在 user.rb 中定义它

    例如

    # Extensions
    devise :database_authenticatable, :registerable, :confirmable,
           :recoverable, :rememberable, :trackable, :validatable, 
           :lastseenable, :timeoutable
    

    考虑:confirmable 符号。

    如果您要更改电子邮件before_updateafter_commit,如果所有条件都返回真值,将调用回调

    你不能从任何地方调用受保护的实例方法。

    如果是受保护的方法,您可以从任何范围内调用它们 属于同一类的对象。这意味着当您通过 Person 对象 p1 作为另一个 Person 对象上的方法的参数 p2,您可以在该方法中访问 p1 的所有受保护方法。

    来自here

    module Confirmable
      extend ActiveSupport::Concern
    
      included do
        # ...
        after_commit :send_on_create_confirmation_instructions, on: :create, if: :send_confirmation_notification?
        after_commit :send_reconfirmation_instructions, on: :update, if: :reconfirmation_required?
        #...
        before_update :postpone_email_change_until_confirmation_and_regenerate_confirmation_token, if: :postpone_email_change?
      end
    

    here是调用回调的地方。

    然后devise会发送确认邮件到user.unconfirmed_email

    您可能需要覆盖重新确认说明电子邮件。这是一个例子:

    # app/mailers/devise/mailer.rb
    
    if defined?(ActionMailer)
      class Devise::Mailer < Devise.parent_mailer.constantize
        include Devise::Mailers::Helpers
    
        def confirmation_instructions(record, token, opts = {})
          @token = token
          if record.pending_reconfirmation?
            devise_mail(record, :reconfirmation_instructions, opts)
          else
            devise_mail(record, :confirmation_instructions, opts)
          end
        end
      end
    end
    

    还有邮件视图

    # app/views/devise/reconfirmation_instructions.text.erb
    
    Hello, <%= @resource.fullname %>!
    
    Please reconfirm your new email: <%= your_url_with_@token" %>
    
    --
    WBR, support team
    

    希望对您有所帮助。

    更新

    启用确认/重新确认只需将所需的策略添加到您的User 模型中

    devise :database_authenticatable, :registerable, :confirmable,
           :recoverable, :rememberable, :trackable, :validatable, 
           :lastseenable, :timeoutable
    

    并覆盖设计邮件程序

    # app/mailers/devise/mailer.rb
    
    if defined?(ActionMailer)
      class Devise::Mailer < Devise.parent_mailer.constantize
        include Devise::Mailers::Helpers
    
        def confirmation_instructions(record, token, opts = {})
          @token = token
          if record.pending_reconfirmation?
            devise_mail(record, :reconfirmation_instructions, opts)
          else
            devise_mail(record, :confirmation_instructions, opts)
          end
        end
      end
    end
    

    【讨论】:

    • 嗨@retgoat - 你的意思是我需要让模块自己确认(你上面显示的方式)还是已经可用的东西 - 因为在用户中添加了可确认的设计型号?
    • 嗨@user2860931,设计已经有了那个模块。我更新了我的答案以更清楚。
    猜你喜欢
    • 1970-01-01
    • 2011-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多