Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a great discussion. Overriding the method as benoror suggests will work great. If you think you might want to capture other user events, then as some others have suggested elsewhere an Observer class might be the cleanest approach. This solution is for Rails 3.0.x and 3.1.</p> <p>To set up an observer, you make the following changes to your application file, adding this observer to any others you might already have.</p> <pre><code>#config/application.rb config.active_record.observers = :user_observer </code></pre> <p>Then create a new file in the models directory:</p> <pre><code>#app/models/user_observer.rb class UserObserver &lt; ActiveRecord::Observer def after_create(user) Notifier.user_new(user).deliver end end </code></pre> <p>If you have a cucumber test that exercises the create user functions, you can add this step to that feature and back it up with a worker step to check for an email in the test mail array.</p> <pre><code>#features/users/sign_up.feature for example Scenario: User signs up with valid data ... And I should receive an email with "[Text from your welcome message]" #features/common_steps.rb Then /^I should receive an email with "([^"]*)"$/ do |value| # this will get the most recent email, so we can check the email headers and body. ActionMailer::Base.deliveries.should_not be_empty @email = ActionMailer::Base.deliveries.last @email.body.should include(value) #@email.from.should == ["no-reply@example.com"] end </code></pre> <p>You environments/test.rb should have these settings to build a mail array instead of sending:</p> <pre><code>config.action_mailer.delivery_method = :test config.action_mailer.perform_deliveries = true </code></pre> <p>Needless to say you can test for much more (to, from, etc) in the message, but this will get your started in a BDD manner if you are so inclined.</p> <p>See also some older StackOverflow threads with insight into this question include:</p> <ul> <li><a href="https://stackoverflow.com/questions/4140378/how-can-i-send-a-welcome-email-to-newly-registered-users-in-rails-using-devise">How can I send a welcome email to newly registered users in Rails using Devise?</a></li> <li><a href="https://stackoverflow.com/questions/3917788/ror-devise-gem-welcome-email-on-sign-up">Using Rails and Devise, I want to send a welcome email on sign up.</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload