Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

attr_accessor not accessible in mailer when using deliver_later

Tags:

I am using devise_invitable to invite users to app, for that I want to add custom message on the email. so my code looks like this.

#user.rb
attr_accessor :message

def self.invite(emails, message)
  User.invite!({email: email}) do |user|
    user.message = message
  end
end

#here I am overriding devise_invitable method to send mails later
def send_devise_notification(notification, *args)
  devise_mailer.send(notification, self, *args).deliver_later
end

#users_controller.rb
def invite_user
  User.invite(params[:email], params[:message]) 
end    

#invitation_instructions.html.erb
<% if @resource.message %>
  <p><%= @resource.message %></p>
<% end %>    

When I am using only deliver then message is displaying on mailer, but when I use deliver_later attr_accessor in nil on the mailer view.

like image 713
power Avatar asked Jul 03 '16 14:07

power


1 Answers

attr_accessor doesn't work because deliver_later serializes your User object to a GlobalID. When the delivery job runs, the job process wil re-load your user from the database (which of course does not include any instance variables on the object). You probably need to pass the message to the job (maybe you could sneak it into *args, (hand

like image 54
cam Avatar answered Sep 28 '22 02:09

cam