ActiveMailer .with method
Hi everyone,
What is the reasoning to use .with over the just passing the args using regular ways?
for ex:
class UserMailer < ApplicationMailer
def welcome_email
@user = params[:user]
mail(to: @user.email, subject: 'Welcome to My Awesome Site')
end
end
Then using it like so UserMailer.with(user: User.first).welcome_email.deliver_now, and the other way:
class UserMailer < ApplicationMailer
def welcome_email(user)
@user = user
mail(to: @user.email, subject: 'Welcome to Our Service')
end
end
Using it like so UserMailer.welcome_email(User.first).deliver_now . Seems like the same thing with extra steps. Is there an entry in the changelog or upgrade guide why use .with?
With params
it's more consistent with Controllers, but also you don't have to assign instance variables like you're doing. You can use params[:user]
right in your template.
def welcome_email
mail(to: params[:user].email, subject: 'Welcome to My Awesome Site')
end
Hey <%= params[:user].first_name %>,