aboutsummaryrefslogtreecommitdiffstats
path: root/guides
diff options
context:
space:
mode:
authorKasper Timm Hansen <kaspth@gmail.com>2017-09-24 18:46:28 +0200
committerKasper Timm Hansen <kaspth@gmail.com>2017-09-24 18:46:28 +0200
commitabd4fd43692cd883068ad27f620fd4c00e546f91 (patch)
tree5b4a306e5688e99ac6f542d102c9ab3c181f4f46 /guides
parentf6b3d34a1c7d99731458137ecaa52b00a412a7be (diff)
downloadrails-abd4fd43692cd883068ad27f620fd4c00e546f91.tar.gz
rails-abd4fd43692cd883068ad27f620fd4c00e546f91.tar.bz2
rails-abd4fd43692cd883068ad27f620fd4c00e546f91.zip
[ci skip] Fix the with order and explain it.
Diffstat (limited to 'guides')
-rw-r--r--guides/source/action_mailer_basics.md11
1 files changed, 8 insertions, 3 deletions
diff --git a/guides/source/action_mailer_basics.md b/guides/source/action_mailer_basics.md
index a025fb773a..cb07781d1c 100644
--- a/guides/source/action_mailer_basics.md
+++ b/guides/source/action_mailer_basics.md
@@ -176,7 +176,7 @@ $ bin/rails db:migrate
Now that we have a user model to play with, we will just edit the
`app/controllers/users_controller.rb` make it instruct the `UserMailer` to deliver
an email to the newly created user by editing the create action and inserting a
-call to `UserMailer.welcome_email` right after the user is successfully saved.
+call to `UserMailer.with(user: @user).welcome_email` right after the user is successfully saved.
Action Mailer is nicely integrated with Active Job so you can send emails outside
of the request-response cycle, so the user doesn't have to wait on it:
@@ -191,7 +191,7 @@ class UsersController < ApplicationController
respond_to do |format|
if @user.save
# Tell the UserMailer to send a welcome email after save
- UserMailer.welcome_email.with(user: @user).deliver_later
+ UserMailer.with(user: @user).welcome_email.deliver_later
format.html { redirect_to(@user, notice: 'User was successfully created.') }
format.json { render json: @user, status: :created, location: @user }
@@ -226,6 +226,11 @@ class SendWeeklySummary
end
```
+Any key value pair passed to `with` just becomes the `params` for the mailer
+action. So `with(user: @user, account: @user.account)` makes `params[:user]` and
+`params[:account]` available in the mailer action. Just like controllers have
+params.
+
The method `welcome_email` returns an `ActionMailer::MessageDelivery` object which
can then just be told `deliver_now` or `deliver_later` to send itself out. The
`ActionMailer::MessageDelivery` object is just a wrapper around a `Mail::Message`. If
@@ -477,7 +482,7 @@ special URL that renders them. In the above example, the preview class for
```ruby
class UserMailerPreview < ActionMailer::Preview
def welcome_email
- UserMailer.welcome_email.with(user: User.first)
+ UserMailer.with(user: User.first).welcome_email
end
end
```