0
0
Ruby on Railsframework~20 mins

Mailer generation and templates in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mailer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this mailer method?
Given the following Rails mailer method, what will be the subject line of the sent email?
Ruby on Rails
class UserMailer < ApplicationMailer
  def welcome_email(user)
    @user = user
    mail(to: @user.email, subject: "Welcome to Our Site")
  end
end

# Assume user.email = 'test@example.com'

# When UserMailer.welcome_email(user).deliver_now is called
A"Welcome Email"
B"Welcome to Our Site"
C"Hello from Our Site"
D"UserMailer Notification"
Attempts:
2 left
💡 Hint
Look at the subject argument inside the mail method.
📝 Syntax
intermediate
1:30remaining
Which option correctly defines a mailer template file name?
In Rails, which filename correctly matches the mailer method 'notification_email' in 'UserMailer' for an HTML email template?
Aapp/views/user_mailer/notification_email.html
Bapp/views/user_mailer/notification_email.html.haml
Capp/views/user_mailer/notification_email.erb
Dapp/views/user_mailer/notification_email.html.erb
Attempts:
2 left
💡 Hint
Rails expects template files to have the mailer method name and format with extension.
🔧 Debug
advanced
2:30remaining
Why does this mailer raise an error?
Examine this mailer method and identify the cause of the error when sending the email.
Ruby on Rails
class UserMailer < ApplicationMailer
  def alert_email(user)
    @user = user
    mail(to: user.email, subject: "Alert") do |format|
      format.html
      format.text
    end
  end
end

# Error: Missing template user_mailer/alert_email.html.erb
ANo template file named alert_email.html.erb exists in user_mailer views folder
BThe mail method is missing the 'from' parameter
CThe block syntax with format is invalid in mail method
DThe user variable is not defined inside the method
Attempts:
2 left
💡 Hint
Check the error message about missing template.
state_output
advanced
2:00remaining
What is the value of @greeting in this mailer template?
Given this mailer method and template, what will be the rendered value of @greeting in the email?
Ruby on Rails
class UserMailer < ApplicationMailer
  def custom_email(user)
    @greeting = "Hello, #{user.name}!"
    mail(to: user.email, subject: "Custom Greeting")
  end
end

# Template: app/views/user_mailer/custom_email.html.erb
# <p><%= @greeting %></p>

# Assume user.name = 'Alice'
A"Hello, Alice!"
B"Hello, user.name!"
C"Hello, !"
D"Hello, #{user.name}!"
Attempts:
2 left
💡 Hint
Instance variables set in the mailer method are available in the template.
🧠 Conceptual
expert
3:00remaining
Which option best describes how Rails mailers handle multipart emails?
How does Rails decide which email format (HTML or text) to send when a mailer defines both formats in its template?
ARails sends only the text part if both templates exist, ignoring the HTML part
BRails sends only the HTML part if both templates exist, ignoring the text part
CRails sends a multipart email containing both HTML and text parts; the email client chooses which to display
DRails randomly sends either the HTML or text part each time the email is sent
Attempts:
2 left
💡 Hint
Think about how email clients handle different content types.