0

我是Rails的新手,並試圖獲得一個示例,使用Rails 4和設計工具向確認電子郵件註冊。我使用的這個例子:啓用電子郵件延遲註冊actionMailer 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 => "[email protected]", 
    :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 protected]", :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 

但我不知道我需要添加此trigg呃。我需要在控制器中執行此操作嗎?或者在視圖中?

非常感謝

回答

1

管理使用來解決這個問題我自己mailcatcher

步驟來解決:

  1. 克隆GitHub的項目lazy_registrations
  2. 創業板安裝mailcatcher
  3. 添加行/environments/development.rb

    config.action_mailer.delivery_method =:SMTP config.action_mailer.smtp_settings = {:地址=> 「本地主機」,:端口=> 1025}

  4. 運行mailcatcher

  5. 檢查電子郵件使用127.0.0.1: 1080

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