设计 /动作仪发送重复的电子邮件以进行注册确认



我的Rails应用程序使用设计来处理注册,身份验证等。我正在使用可确认的模块。错误是 - 当用户注册电子邮件时,Deise正在发送两个带有不同确认链接的确认电子邮件。一个链接起作用,另一个链接将用户引导到错误页面。

设计与错误关联的消息:"确认令牌无效",并将用户带到重新启动确认电子邮件页面。

我正在与Heroku主持,并使用SendGrid发送电子邮件。更新:该错误也发生在Localhost上。

我不知道此错误的根源在哪里,这可能比您需要看到的更多代码:


Models/user.rb

...
devise :database_authenticatable, :registerable, :omniauthable,
     :recoverable, :rememberable, :trackable, :validatable, 
     :confirmable, :authentication_keys => [:login]
...
## callbacks
after_create :account_created
# called after the account is first created
def account_created
  # check if this activiy has already been created
  if !self.activities.where(:kind => "created_account").blank?
    puts "WARNING: user ##{self.id} already has a created account activity!"
    return
  end
  # update points
  self.points += 50
  self.save
  # create activity
  act = self.activities.new
  act.kind = "created_account"
  act.created_at = self.created_at
  act.save
end
...
def confirmation_required?
  super && (self.standard_account? || self.email_changed)
end
...



控制器/registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController
  def update
    unless @user.last_sign_in_at.nil?
      puts "--------------double checking whether password confirmation is required--"
      ## if the user has not signed in yet, we don't want to do this.
      @user = User.find(current_user.id)
      # uncomment if you want to require password for email change
      email_changed = @user.email != params[:user][:email]
      password_changed = !params[:user][:password].empty?
      # uncomment if you want to require password for email change
      # successfully_updated = if email_changed or password_changed
      successfully_updated = if password_changed
        params[:user].delete(:current_password) if params[:user][:current_password].blank?
        @user.update_with_password(params[:user])
      else
        params[:user].delete(:current_password)
        @user.update_without_password(params[:user])
      end
      if successfully_updated
        # Sign in the user bypassing validation in case his password changed
        sign_in @user, :bypass => true
        if email_changed
          flash[:blue] = "Your account has been updated! Check your email to confirm your new address. Until then, your email will remain unchanged."
        else
          flash[:blue] = "Account info has been updated!"
        end
        redirect_to edit_user_registration_path
      else
        render "edit"
      end
    end
  end
end



控制器/omniauth_callbacks_controller

class OmniauthCallbacksController < Devise::OmniauthCallbacksController
  skip_before_filter :verify_authenticity_token
    def facebook
        user = User.from_omniauth(request.env["omniauth.auth"])
    if user.persisted?
      flash.notice = "Signed in!"
      # if the oauth_token is expired or nil, update it...
      if (DateTime.now > (user.oauth_expires_at || 99.years.ago) )
        user.update_oauth_token(request.env["omniauth.auth"])
      end
      sign_in_and_redirect user
    else
      session["devise.user_attributes"] = user.attributes
      redirect_to new_user_registration_url
    end
    end
end



config/utaes.rb

...
devise_for :users, controllers: {omniauth_callbacks: "omniauth_callbacks", 
                                :registrations => "registrations"}
...

如果需要,我很高兴提供更多信息。我也愿意自定义/覆盖设计的邮件行为,但我不知道该怎么做。

非常感谢!

已解决!

我能够替代设计:: Mailer并强制堆栈跟踪,以确切找出导致重复电子邮件的原因。设计:: Mailer#cresence_instructions被称为两次,我发现我的问题与我:after_create回调,如下:


在模型/user.rb中...

after_create :account_created
# called after the account is first created
def account_created
...
  # update points
  self.points += 50
  self.save
...
end

呼叫self.save以某种方式导致邮件机再次触发。我通过添加点时更改来解决问题。我摆脱了after_create的电话,并覆盖了确认!设计的方法看起来像这样:

def confirm!
  super
  account_created
end

因此,直到确认后,用户记录才不会被修改(添加点)。不再重复电子邮件!

我最初和托马斯·克莱姆(Thomas Klemm)的答案一起去了,但是当我有业余时间尝试弄清楚发生了什么时,我回去看这个。

我跟踪了"问题",并注意到它只有在您设计(用户)模型中设置可确认的(用户)模型和可重新确认的初始化器中才会发生 - 事后,这是很有意义的,因为本质上是在After_create中我们正在更改用户模型,尽管我们没有更改电子邮件地址 - 我怀疑设计可能会这样做,因为该帐户尚未确认,但是在任何情况下>self.skip_reconfirmation!在after_create方法中。

我创建了一个样本导轨项目,并进行了两次测试,以确保正确的行为。以下是关键摘录。如果您手上有太多时间,可以在此处查看该项目:https://github.com/richhollis/devise-reconfirmable-test

app/models/user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable
  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me
  after_create :add_attribute
  private
  def add_attribute
    self.skip_reconfirmation!
    self.update_attributes({ :status => 200 }, :without_protection => true)
  end
end

initializers/deaise.rb

# Use this hook to configure devise mailer, warden hooks and so forth.
# Many of these configuration options can be set straight in your model.
Devise.setup do |config|
  ..
  ..
  # If true, requires any email changes to be confirmed (exactly the same way as
  # initial account confirmation) to be applied. Requires additional unconfirmed_email
  # db field (see migrations). Until confirmed new email is stored in
  # unconfirmed email column, and copied to email column on successful confirmation.
  config.reconfirmable = true
  ..
  ..
end

spec/models/user_spec.rb

require 'spec_helper'
describe User do
  subject(:user) { User.create(:email => 'nobody@nobody.com', :password => 'abcdefghijk') }
  it "should only send one email during creation" do
    expect {
      user
    }.to change(ActionMailer::Base.deliveries, :count).by(1)
  end
  it "should set attribute in after_create as expected" do
    user.status.should eq(200)
  end
end

运行RSPEC测试以确保仅发送一封电子邮件确认行为:

..

在0.87571秒完成2个示例,0失败

感谢您的出色解决方案,Stephen!我已经尝试过它,它可以完美地挂接到confirm!方法中。但是,在这种情况下,当用户单击他收到的电子邮件中的确认链接时,将调用该功能(如名称)。

一种替代方法是挂接generate_confirmation_token方法,因此创建确认令牌并发送电子邮件时,您的方法直接调用。

# app/models/user.rb
def generate_confirmation_token
  make_owner_an_account_member
  super # includes a call to save(validate: false), 
        # so be sure to call whatever you like beforehand
end
def make_owner_an_account_member
  self.account = owned_account if owned_account?
end

确认模块的相关来源。

相关内容

  • 没有找到相关文章

最新更新