2011-11-21 73 views
0

因此,當用戶註冊網站時,我設置了一個歡迎信息 - 之前我已經使用gmail設置了它(http://stackoverflow.com/questions/5793296/rails-actionmailer-w -devise-google-apps-in-development-mode),但它會使用谷歌應用 - 所以如果我是正確的另一個stackoverflow用戶聲稱設置是相似的,所以這不是一個問題。但因爲我只想收到一封歡迎郵件,所以我想我可以只使用可確認的設置,以便他們收到一封電子郵件,然後在配置中設置它,以便用戶不必在確定1000年之後確認大的基本上這不是一封確認郵件? (如果有更好的方法來做到這一點,我會很感激這樣的輸入)設計確認 - 歡迎信息

回答

4

你不需要扭曲Confirmable特性來實現這一點,你可以用ActiveRecord::Observer更優雅地做到這一點。基本上,當你註冊/保存一個用戶時,觀察者會收到通知,並從那裏你可以打電話給郵件。你可以在下面看到一個例子。

應用程序/郵寄/ user_mailer.rb

class UserMailer < ActionMailer::Base 
    default from: "[email protected]" 

    def welcome_mail(email) 
    mail(:to => email, :subject => "Welcome to Something").deliver 
    end 
end 

應用程序/模型/ user_observer.rb

class UserObserver < ActiveRecord::Observer 
    # We check if it's a new user 
    def before_save(user) 
    @is_new_record = user.new_record? 
    true 
    end 

    def after_save(user) 
    # If it's not a new user we don't want to send them another welcome email 
    if @is_new_record then 
     UserMailer.welcome_mail(user.email) 
    end 
    end 
end 

最後,你需要配置軌註冊觀察者。

的config/application.rb中(僅提取物)

config.active_record.observers = :user_observer 
+0

好的,非常感謝您的詳細回覆。 – eWizardII

0

它可能是實在太遲了回答,但我認爲有after_create回調萎縮上面,因爲你並不需要檢查的解決方案這是一個新紀錄!