Performance: Mailer generation and templates
This affects the server response time and initial email generation speed, impacting how fast emails are prepared and sent.
class UserMailer < ApplicationMailer def welcome_email(user) @user = user mail(to: @user.email, subject: 'Welcome!') end end # In app/views/user_mailer/welcome_email.html.erb <h1>Welcome, <%= @user.name %>!</h1> # In app/views/user_mailer/welcome_email.text.erb Welcome, <%= @user.name %>!
class UserMailer < ApplicationMailer def welcome_email(user) @user = user mail(to: @user.email, subject: 'Welcome!') do |format| format.html { render inline: "<h1>Welcome, <%= @user.name %>!</h1>" } format.text { render plain: "Welcome, #{@user.name}!" } end end end
| Pattern | Template Parsing | CPU Load | I/O Blocking | Verdict |
|---|---|---|---|---|
| Inline ERB in mailer method | High (per email) | High | Low | [X] Bad |
| Precompiled ERB templates | Low (once) | Low | Low | [OK] Good |
| Synchronous large file read | Low | Medium | High | [X] Bad |
| Background job for attachments | Low | Low | Low | [OK] Good |
UserMailer?rails generate mailer UserMailer creates a mailer class and related files.class UserMailer < ApplicationMailer
def welcome_email(user)
@user = user
mail(to: @user.email, subject: 'Welcome!')
end
endUserMailer.welcome_email(@user).deliver_now is called?welcome_email sets instance variables and calls mail to prepare the email.deliver_nowdeliver_now sends the email immediately instead of queuing it.welcome_email.html.erb:<h1>Welcome, <%= @user.name %>!</h1> <p>Thanks for joining.</p> <p>Your email is: <%= user.email %></p>
@user, so the template must use @user to access user data.user.email which is undefined, causing an error..html.erb and .text.erb extensions.Pass the test and this pattern moves unknown