0

我是 Rails 的新手,并试图获得一个使用 Rails 4 的确认电子邮件注册并设计的示例。我正在使用这个例子:

https://github.com/mwlang/lazy_registration_demos

我创建了以下文件:

初始化程序/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>

确认控制器.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

但我不知道我需要在哪里添加这个触发器。我需要在控制器中执行此操作吗?还是在视野中?

非常感谢

4

1 回答 1

1

使用mailcatcher自己解决了这个问题

修复步骤:

  1. 克隆 github 项目lazy_registrations
  2. 宝石安装邮件捕手
  3. 向 /environments/development.rb 添加行

    config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025 }

  4. 运行邮件捕手

  5. 使用 127.0.0.1:1080 检查电子邮件

忽略上面的所有其他代码,只需要睡一晚:)

于 2014-07-15T09:50:46.947 回答