0
0
Ruby on Railsframework~20 mins

Action Mailer setup in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Action Mailer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when you call deliver_now on a mailer method?

Consider a Rails mailer method that sends an email. What is the behavior when you call deliver_now on the mailer method?

Ruby on Rails
UserMailer.welcome_email(user).deliver_now
AThe email is sent immediately during the current request.
BThe email is discarded and not sent.
CThe email is saved to the database but not sent.
DThe email is queued to be sent later by a background job.
Attempts:
2 left
💡 Hint

Think about synchronous vs asynchronous email sending.

📝 Syntax
intermediate
2:00remaining
Which option correctly sets the default 'from' email in a mailer class?

In a Rails mailer class, how do you set the default sender email address for all emails?

Ruby on Rails
class UserMailer < ApplicationMailer
  # set default from here
end
Afrom default: 'no-reply@example.com'
Bset_default from: 'no-reply@example.com'
Cdefault_from 'no-reply@example.com'
Ddefault from: 'no-reply@example.com'
Attempts:
2 left
💡 Hint

Look for the correct method name and syntax for setting defaults.

🔧 Debug
advanced
2:00remaining
Why does this mailer method raise an error?

Given the following mailer method, why does calling UserMailer.notify(user).deliver_now raise an error?

Ruby on Rails
class UserMailer < ApplicationMailer
  def notify(user)
    mail(to: user.email, subject: 'Notification') do |format|
      format.text { render plain: 'Hello!' }
      format.html { render html: '<strong>Hello!</strong>'.html_safe }
    end
  end
end
AThe block passed to <code>mail</code> is missing a closing <code>end</code>.
BThe <code>html_safe</code> method is not allowed inside mailer views.
CThe <code>render html:</code> call requires a template, not a string.
DThe mailer method is missing a <code>return</code> statement.
Attempts:
2 left
💡 Hint

Check how render works inside mailers.

state_output
advanced
2:00remaining
What is the value of @greeting in the mailer view after calling this method?

Consider this mailer method:

def welcome(user)
  @greeting = "Hello, #{user.name}!"
  mail(to: user.email, subject: 'Welcome')
end

What will @greeting contain in the mailer view?

AThe string "Hello, " followed by the user's name and an exclamation mark.
BNil, because instance variables are not passed to mailer views.
CAn empty string, since <code>@greeting</code> is not set in the view.
DThe user's email address.
Attempts:
2 left
💡 Hint

Think about how instance variables work in mailers and views.

🧠 Conceptual
expert
3:00remaining
Which configuration is required to send emails asynchronously using Active Job with Action Mailer?

You want to send emails without blocking the web request by using background jobs. Which configuration and method call combination achieves this in Rails?

ASet <code>config.action_mailer.delivery_method = :sendmail</code> and call <code>deliver_now</code> on the mailer method.
BSet <code>config.action_mailer.delivery_method = :smtp</code> and call <code>deliver_later</code> on the mailer method.
CSet <code>config.action_mailer.perform_deliveries = false</code> and call <code>deliver_later</code> on the mailer method.
DSet <code>config.active_job.queue_adapter = :inline</code> and call <code>deliver_now</code> on the mailer method.
Attempts:
2 left
💡 Hint

Consider how to enable background job processing and which mailer method triggers async sending.