What command do you use to generate a new mailer in Rails?
You use rails generate mailer MailerName to create a new mailer with default files.
Click to reveal answer
beginner
Where do Rails mailer templates live?
Mailer templates are stored in app/views/mailer_name/ folder, with one file per email action.
Click to reveal answer
intermediate
How do you define an email method inside a Rails mailer?
Inside the mailer class, define a method that sets instance variables and calls mail(to:, subject:) to send the email.
Click to reveal answer
beginner
What file extension do Rails mailer templates use for HTML emails?
Rails uses .html.erb files for HTML email templates, combining HTML with embedded Ruby.
Click to reveal answer
intermediate
How can you preview emails in Rails during development?
You can preview emails by creating a preview class in <code>test/mailers/previews</code> and visiting <code>http://localhost:3000/rails/mailers</code>.
Click to reveal answer
Which command generates a mailer named 'UserMailer' in Rails?
Arails generate mailer UserMailer
Brails generate model UserMailer
Crails generate controller UserMailer
Drails generate job UserMailer
✗ Incorrect
The correct command to generate a mailer is rails generate mailer UserMailer.
Where do you place the email template files for a mailer called 'NotificationMailer'?
Aapp/controllers/notification_mailer/
Bapp/mailers/notification_mailer/
Capp/views/notification_mailer/
Dapp/assets/notification_mailer/
✗ Incorrect
Email templates go inside app/views/notification_mailer/ folder.
What method do you call inside a mailer method to send an email?
Asend_email()
Bmail()
Cdeliver_now()
Demail_send()
✗ Incorrect
Inside a mailer method, you call mail(to:, subject:) to prepare the email.
Which file extension is used for plain text email templates in Rails?
A.text.plain
B.text.erb
C.html.erb
D.txt.erb
✗ Incorrect
Plain text email templates use .txt.erb extension.
How can you preview emails in Rails without sending them?
ABy visiting <code>/rails/mailers</code> in the browser during development
BUsing <code>rails mailer preview</code> command
CEmails cannot be previewed without sending
DUsing <code>rails email preview</code> gem only
✗ Incorrect
You can preview emails by visiting http://localhost:3000/rails/mailers in development.
Explain the steps to create and send an email using a Rails mailer.
Think about generating, coding the method, templates, and sending.
You got /6 concepts.
Describe how Rails mailer templates work and where they are stored.
Focus on folder location, file types, and how templates use Ruby.
You got /5 concepts.
Practice
(1/5)
1. What is the primary purpose of a mailer in a Rails application?
easy
A. To send emails from the application
B. To handle database migrations
C. To manage user sessions
D. To style HTML templates
Solution
Step 1: Understand the role of mailers
Mailers in Rails are designed to send emails from the application to users or other recipients.
Step 2: Differentiate from other components
Database migrations handle schema changes, sessions manage user logins, and styling is done in views or CSS, not mailers.
Final Answer:
To send emails from the application -> Option A
Quick Check:
Mailer = send emails [OK]
Hint: Mailers send emails, not handle data or styling [OK]
Common Mistakes:
Confusing mailers with controllers
Thinking mailers manage database
Assuming mailers style HTML
2. Which Rails command correctly generates a mailer named UserMailer?
easy
A. rails generate controller UserMailer
B. rails generate mailer UserMailer
C. rails generate model UserMailer
D. rails generate job UserMailer
Solution
Step 1: Identify the correct generator for mailers
The command rails generate mailer UserMailer creates a mailer class and related files.
Step 2: Understand other generators
Controllers handle web requests, models handle data, and jobs handle background tasks, so those commands are incorrect for mailers.
Final Answer:
rails generate mailer UserMailer -> Option B
Quick Check:
Mailer generator uses 'mailer' keyword [OK]
Hint: Use 'rails generate mailer' to create mailers [OK]
Common Mistakes:
Using controller generator for mailers
Confusing model and mailer generators
Typing job instead of mailer
3. Given this mailer method:
class UserMailer < ApplicationMailer
def welcome_email(user)
@user = user
mail(to: @user.email, subject: 'Welcome!')
end
end
What happens when UserMailer.welcome_email(@user).deliver_now is called?
medium
A. The email is queued but not sent immediately
B. The method returns a string with the email content but does not send it
C. An email is sent immediately to the user's email with subject 'Welcome!'
D. An error occurs because deliver_now is not a valid method
Solution
Step 1: Understand mailer method behavior
The method welcome_email sets instance variables and calls mail to prepare the email.
Step 2: Effect of deliver_now
Calling deliver_now sends the email immediately instead of queuing it.
Final Answer:
An email is sent immediately to the user's email with subject 'Welcome!' -> Option C
Quick Check:
deliver_now sends email immediately [OK]
Hint: deliver_now sends email immediately, deliver_later queues it [OK]
Common Mistakes:
Thinking deliver_now only prepares email
Confusing deliver_now with deliver_later
Assuming deliver_now is invalid
4. Identify the error in this mailer template file named welcome_email.html.erb:
<h1>Welcome, <%= @user.name %>!</h1>
<p>Thanks for joining.</p>
<p>Your email is: <%= user.email %></p>
medium
A. The = sign is missing in the ERB tags
B. The HTML tags are incorrect and will cause rendering errors
C. The template file should be named welcome_email.text.erb
D. The variable user is undefined; should use @user
Solution
Step 1: Check variable usage in template
The mailer method sets @user, so the template must use @user to access user data.
Step 2: Identify incorrect variable
The template uses user.email which is undefined, causing an error.
Final Answer:
The variable user is undefined; should use @user -> Option D
Quick Check:
Use instance variables (@user) in mailer templates [OK]
Hint: Mailer templates use instance vars like @user, not local vars [OK]
Common Mistakes:
Using local variables instead of instance variables
Misnaming template files
Forgetting <%= in ERB tags
5. You want to send a welcome email with both HTML and plain text versions. Which setup correctly supports this in Rails mailers?
hard
A. Create welcome_email.html.erb and welcome_email.text.erb templates in the mailer views folder
B. Create only welcome_email.html.erb template; Rails auto-generates text version
C. Create welcome_email.erb with mixed HTML and text content
D. Create welcome_email.text.erb only; HTML is optional
Solution
Step 1: Understand multi-format email templates
Rails supports sending emails with both HTML and plain text by providing separate templates named with .html.erb and .text.erb extensions.
Step 2: Why both templates are needed
Some email clients prefer plain text, so having both ensures compatibility and better user experience.
Final Answer:
Create welcome_email.html.erb and welcome_email.text.erb templates in the mailer views folder -> Option A
Quick Check:
Both .html.erb and .text.erb templates needed for multi-format emails [OK]
Hint: Use both .html.erb and .text.erb templates for emails [OK]
Common Mistakes:
Assuming Rails auto-generates text version
Mixing HTML and text in one template
Skipping plain text template
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.