---
title: "Rails 5.2 pass request params to Action Mailer previews"
description:
  "A neat way to pass the params to the Action Mailer previews directly for
  better debugging"
canonical_url: "https://www.bigbinary.com/blog/rails-5-2-allows-passing-request-params-to-action-mailer-previews"
markdown_url: "https://www.bigbinary.com/blog/rails-5-2-allows-passing-request-params-to-action-mailer-previews.md"
---

# Rails 5.2 pass request params to Action Mailer previews

A neat way to pass the params to the Action Mailer previews directly for better
debugging

- Author: Prathamesh Sonpatki
- Published: January 8, 2018
- Categories: Rails 5.2, Rails

Rails has inbuilt feature to preview the emails using
[Action Mailer previews](http://guides.rubyonrails.org/action_mailer_basics.html#previewing-emails).

A preview mailer can be setup as shown here.

```ruby
class NotificationMailer < ApplicationMailer
def notify(email: email, body: body)
user = User.find_by(email: email)
mail(to: user.email, body: body)
end
end

class NotificationMailerPreview < ActionMailer::Preview
def notify
NotificationMailer.notify(email: User.first.email, body: "Hi there!")
end
end
```

This will work as expected. But our email template is displayed differently
based on user's role. To test this, we need to update the notify method and then
check the updated preview.

What if we could just pass the email in the preview URL.

```plaintext
http://localhost:3000/rails/mailers/notification/notify?email=superadmin@example.com
```

In Rails 5.2, we can pass the params directly in the URL and
[params will be available to the preview mailers](https://github.com/rails/rails/pull/28244).

Our code can be changed as follows to use the params.

```ruby
class NotificationMailerPreview < ActionMailer::Preview
def notify
email = params[:email] || User.first.email
NotificationMailer.notify(email: email, body: "Hi there!")
end
end
```

This allows us to test our mailers with dynamic input as per requirements.

## Links

- [Human page](https://www.bigbinary.com/blog/rails-5-2-allows-passing-request-params-to-action-mailer-previews)
