Ask A Question

Notifications

You’re not receiving notifications from this thread.

ActiveMailer .with method

Emir Ibrahimbegovic asked in Rails

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?

Reply

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 %>,
Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,563+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.