启用电子邮件延迟注册actionMailer Rails 4

时间:2014-07-14 22:39:54

标签: ruby-on-rails actionmailer devise-confirmable lazy-registration

我是Rails的新手并尝试通过Rails 4和设计的确认电子邮件注册。我正在使用这个例子:

https://github.com/mwlang/lazy_registration_demos

我创建了以下文件:

initialisers /为setup_mail.rb

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
    :address => "smtp.gmail.com",
    :port => 587,
    :domain => "gmail.com",
    :user_name => "account@gmail.com",
    :password => "passwork",
    :authentication => "plain",
    :enable_starttls_auto => true
}

/app/mailers/welcome_email.rb

class WelcomeMailer < ActionMailer::Base

    def registration_confirmation(user)

        mail :to => user, :from => "email@domain.com", :subject => "Subject line"

    end
end

/devise/mailer/confirmations_instructions.html.erb

<p>
  Welcome #{@email}!
</p>
<p>You can confirm your account email through the link below:</p>
<p>
  <%= link_to 'Confirm my account', confirmation_url(@resource, :confirmation_token => @resource.confirmation_token) %>
</p>

confirmations_controller.rb

class ConfirmationsController < Devise::ConfirmationsController
  # Remove the first skip_before_filter (:require_no_authentication) if you
  # don't want to enable logged users to access the confirmation page.
  skip_before_filter :require_no_authentication
  skip_before_filter :authenticate_user!, except: [:confirm_user]

  def update
    with_unconfirmed_confirmable do
      if confirmable_user.blank_password?
        confirmable_user.update_password(params[:user])
        if confirmable_user.valid?
          do_confirm
        else
          do_show
          confirmable_user.errors.clear # prevent rendering :new
        end
      else
        self.class.add_error_on(self, :email, :password_allready_set)
      end
    end

    render 'devise/confirmations/new' unless confirmable_user.errors.empty?
  end

  def confirm_user
    if confirmation_token && (@confirmable_user = User.find_by(:confirmation_token => confirmation_token))
      do_show
    else
      flash[:error] = "Invalid confirmation token"
      redirect_to :unconfirmed
    end
  end

  def show
    with_unconfirmed_confirmable do
      confirmable_user.blank_password? ? do_show : do_confirm
    end
    unless confirmable_user.errors.empty?
      self.resource = confirmable_user
      render 'devise/confirmations/new' 
    end
  end

  protected

  def confirmation_token
    @confirmation_token ||= params["user"] && params["user"]["confirmation_token"] || params["confirmation_token"]
  end

  def confirmable_user
    @confirmable_user ||= User.find_or_initialize_with_error_by(:confirmation_token, confirmation_token)
  end

  def with_unconfirmed_confirmable
    unless confirmable_user.new_record?
      confirmable_user.only_if_unconfirmed {yield}
    end
  end

  def do_show
    self.resource = confirmable_user
    render 'devise/confirmations/show'
  end

  def do_confirm
    confirmable_user.confirm!
    set_flash_message :notice, :confirmed
    sign_in_and_redirect(resource_name, confirmable_user)
  end
end

/devise/registrations/new.html.haml

%h2 Sign up
= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f|
  = devise_error_messages!
  %div
    = f.label :email
    = f.email_field :email, :autofocus => true
  %div{style: "margin-top: 25px"}= f.submit "Sign up", class: "btn btn-primary btn-large"
%hr
= render "devise/shared/links"

要触发要发送的电子邮件,我需要添加

WelcomeMailer.registration_confirmation(@user).deliver

但我不知道我需要添加此触发器的位置。我需要在控制器中执行此操作吗?或者在视图中?

非常感谢

1 个答案:

答案 0 :(得分:1)

管理我自己使用mailcatcher解决此问题

修复步骤:

  1. clone github project lazy_registrations
  2. gem install mailcatcher
  3. 在/environments/development.rb

    中添加行

    config.action_mailer.delivery_method =:smtp config.action_mailer.smtp_settings = {:address =&gt; “localhost”,:port =&gt; 1025}

  4. 运行mailcatcher

  5. 使用127.0.0.1:1080检查电子邮件
  6. 忽略上面的所有其他代码,只需要一夜睡眠:)