Cannot pass model instance variable to mailer
I have a model that sends out invitations via email by parsing a CSV file
for names and emails. I have a before_create that creates a url and saves
it as an instance variable. After the record is created, it is supposed to
send the results to the mailer, along with the instance variable of the
URL. It seems like the URL is not being sent to the mailer as the emails
are sent successfully, but with the URL. Below are the relevant lines of
code. I am confirm that the invite_token is being created, so that is not
an issue.
Note: I am using SmarterCSV gem to parse the csv and using the
delayed_jobs gem to create a background process.
Let me explain the process:
Controller (not shown) receives the CSV and sends it to Invitation.import.
The file is parsed and before the record is created, an invite token is
created, then a URL is built. The email is then sent.
Thanks!
Model: Invitation.rb
class Invitation < ActiveRecord::Base
before_save { |user| user.email = user.email.downcase }
before_create :create_invite_token
before_create :build_url
def self.import(file, id)
file_path = file.path.to_s
file_csv = SmarterCSV.process(file_path)
file_csv.each do |x|
x[:event_id] = id
Invitation.delay.create! x
UserMailer.delay.invitation_email(x, @url)
end
end
def build_url
@url = 'http://localhost:3000/confirmation/' + self.invite_token
end
private
def create_invite_token
self.invite_token = SecureRandom.urlsafe_base64
end
end
Mailer: user_mailer.rb
def invitation_email(invitation, signup_url)
@invitation = invitation
@signup_url = signup_url
mail(:to => invitation[:email], :subject => "You're invited!")
end
Invitation email:
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
</head>
<body>
<h2>Hi <%= @invitation[:name].split.first %>,</h2>
<p>
Click here: <%= @signup_url %>
</p>
</body>
</html>
No comments:
Post a Comment