Bird
Raised Fist0
Ruby on Railsframework~20 mins

Sending emails from controllers in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Rails Email Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when you call UserMailer.welcome_email.deliver_now inside a Rails controller action?

Consider a Rails controller action that calls UserMailer.welcome_email.deliver_now. What is the immediate effect of this call?

Ruby on Rails
class UsersController < ApplicationController
  def create
    @user = User.create(user_params)
    UserMailer.welcome_email(@user).deliver_now
    redirect_to root_path
  end
end
AThe welcome email is sent immediately before the redirect happens.
BThe welcome email is queued to be sent later by a background job.
CThe email sending is skipped because deliver_now does not send emails.
DThe email is prepared but not sent until the response is fully rendered.
Attempts:
2 left
💡 Hint

Think about what deliver_now means in Rails mailers.

❓ state_output
intermediate
2:00remaining
What is the result of calling UserMailer.welcome_email(@user).deliver_later in a controller?

In a Rails controller, you call UserMailer.welcome_email(@user).deliver_later. What happens to the email sending process?

Ruby on Rails
class UsersController < ApplicationController
  def create
    @user = User.create(user_params)
    UserMailer.welcome_email(@user).deliver_later
    redirect_to root_path
  end
end
AThe email is never sent because deliver_later is not implemented.
BThe email is sent immediately during the controller action.
CThe email is scheduled to be sent asynchronously by Active Job.
DThe email sending blocks the controller until it finishes.
Attempts:
2 left
💡 Hint

Consider how deliver_later works with background jobs in Rails.

📝 Syntax
advanced
2:00remaining
Which option correctly sends a welcome email with parameters from a Rails controller?

You want to send a welcome email passing the @user object to the mailer from a controller. Which code snippet is correct?

AUserMailer.welcome_email(@user).deliver_now
BUserMailer.welcome_email.deliver_now(@user)
CUserMailer.welcome_email().deliver_now(@user)
DUserMailer.welcome_email(user: @user).deliver_now
Attempts:
2 left
💡 Hint

Remember how mailer methods accept arguments in Rails.

🔧 Debug
advanced
2:00remaining
Why does this controller code raise an error when sending email?

Given this controller code, why does it raise an error?

Ruby on Rails
class UsersController < ApplicationController
  def create
    @user = User.create(user_params)
    UserMailer.welcome_email.deliver_now(@user)
    redirect_to root_path
  end
end
AUserMailer.welcome_email requires no arguments, so passing @user causes an error.
Bdeliver_now does not accept any arguments; the user must be passed to welcome_email.
Cdeliver_now must be called without parentheses to work correctly.
DThe redirect_to must come before calling deliver_now to avoid errors.
Attempts:
2 left
💡 Hint

Check the method signatures for mailers and deliver_now.

🧠 Conceptual
expert
2:00remaining
What is the best practice for sending emails in Rails controllers to avoid slowing down user response?

In Rails, sending emails directly in controller actions can slow down the response. What is the recommended approach to avoid this?

ASend emails only after the controller action completes by calling mailers in views.
BSend emails synchronously with <code>deliver_now</code> to ensure immediate delivery.
CAvoid sending emails from controllers; instead, send them from JavaScript on the client side.
DUse <code>deliver_later</code> to enqueue emails for asynchronous sending via Active Job.
Attempts:
2 left
💡 Hint

Think about user experience and server response times.

Practice

(1/5)
1. What is the main role of a controller when sending emails in Rails?
easy
A. To call the mailer method and trigger the email sending
B. To write the email content and layout
C. To configure the SMTP settings
D. To store email templates in the database

Solution

  1. Step 1: Understand controller responsibility

    Controllers handle user requests and trigger actions like sending emails by calling mailer methods.
  2. Step 2: Identify mailer role

    Mailers contain email content and setup, not controllers.
  3. Final Answer:

    To call the mailer method and trigger the email sending -> Option A
  4. Quick Check:

    Controllers trigger mailers [OK]
Hint: Controllers trigger mailers, mailers build emails [OK]
Common Mistakes:
  • Writing email content in controllers
  • Configuring SMTP in controllers
  • Storing templates in controllers
2. Which of the following is the correct syntax to send an email immediately from a controller using a mailer named UserMailer and a method welcome_email?
easy
A. UserMailer.welcome_email.deliver_later(params[:user])
B. UserMailer.welcome_email(params[:user]).deliver_now
C. UserMailer.deliver_now.welcome_email(params[:user])
D. UserMailer.welcome_email(params[:user]).send_now

Solution

  1. Step 1: Identify mailer method call

    The mailer method is called with parameters: UserMailer.welcome_email(params[:user]).
  2. Step 2: Use correct delivery method

    To send immediately, use deliver_now after the mailer method call.
  3. Final Answer:

    UserMailer.welcome_email(params[:user]).deliver_now -> Option B
  4. Quick Check:

    Immediate send = deliver_now after mailer call [OK]
Hint: Call mailer method then add .deliver_now for immediate send [OK]
Common Mistakes:
  • Using deliver_later for immediate send
  • Calling deliver_now before mailer method
  • Using non-existent send_now method
3. Given this controller code snippet:
def create
  @user = User.new(user_params)
  if @user.save
    UserMailer.welcome_email(@user).deliver_later
    redirect_to root_path, notice: 'User created!'

What happens when this code runs successfully?
medium
A. The welcome email is sent immediately before redirecting
B. The redirect happens only after the email is sent
C. The welcome email is queued to be sent later, then redirect happens
D. The email is never sent because deliver_later is incorrect here

Solution

  1. Step 1: Understand deliver_later behavior

    Calling deliver_later queues the email to be sent asynchronously in the background.
  2. Step 2: Analyze controller flow

    The redirect happens immediately after queuing the email, not waiting for sending.
  3. Final Answer:

    The welcome email is queued to be sent later, then redirect happens -> Option C
  4. Quick Check:

    deliver_later queues email, redirect immediate [OK]
Hint: deliver_later queues email, does not block controller [OK]
Common Mistakes:
  • Thinking deliver_later sends email immediately
  • Assuming redirect waits for email sending
  • Believing deliver_later is invalid syntax
4. Identify the error in this controller code snippet:
def notify
  UserMailer.notify_user.deliver_now(user)
  redirect_to dashboard_path
end
medium
A. deliver_now is not a valid method for sending emails
B. redirect_to should be called before sending email
C. notify_user method cannot be called without parentheses
D. deliver_now should be called after passing user argument to notify_user

Solution

  1. Step 1: Check method call order

    The user argument must be passed to notify_user before calling deliver_now.
  2. Step 2: Correct syntax

    Correct call: UserMailer.notify_user(user).deliver_now.
  3. Final Answer:

    deliver_now should be called after passing user argument to notify_user -> Option D
  4. Quick Check:

    Pass args before deliver_now [OK]
Hint: Pass arguments inside mailer method, then call deliver_now [OK]
Common Mistakes:
  • Calling deliver_now with arguments
  • Calling redirect_to before email
  • Misusing parentheses in method calls
5. You want to send a confirmation email from a controller only if the user has a verified email address. Which is the best way to implement this?
hard
A. In the controller, check if user.verified? then call UserMailer.confirmation_email(user).deliver_now
B. Always call UserMailer.confirmation_email(user).deliver_now and check verification inside the mailer
C. Send the email unconditionally and add a condition in the email template
D. Call UserMailer.confirmation_email.deliver_now(user) without any condition

Solution

  1. Step 1: Decide where to check condition

    Controller should decide if email should be sent based on user verification status.
  2. Step 2: Call mailer only when condition is true

    Call UserMailer.confirmation_email(user).deliver_now only if user.verified? is true.
  3. Final Answer:

    In the controller, check if user.verified? then call UserMailer.confirmation_email(user).deliver_now -> Option A
  4. Quick Check:

    Check condition in controller before sending email [OK]
Hint: Check conditions in controller before calling mailer [OK]
Common Mistakes:
  • Checking conditions inside mailer or template
  • Sending emails unconditionally
  • Passing arguments after deliver_now
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard