2

I am attempting to create beta invitations using the structure from railscasts episode 124, updated for rails 3.2.8.

Currently, the invitation email gets sent, but does not contain the url (which includes the invitation token) for users to follow to sign up because the instance variable I am creating in ActionMailer (@invitation_link) is nil in the view. Inspecting @invitation_link in the ActionMailer controller shows that it is pointing to the correct url, but it is nil in the view.

I have also checked out the following questions and none of the solutions have worked for me:

How do you use an instance variable with mailer in Ruby on Rails?

https://stackoverflow.com/questions/5831038/unable-to-access-instance-variable-in-mailer-view

Actionmailer instance variable problem Ruby on Rails

ActionMailer pass local variables to the erb template

Relevant code snippets below:

invitations_controller.rb

class InvitationsController < ApplicationController
  def new
    @invitation = Invitation.new
  end

  def create
    @invitation = Invitation.new(params[:invitation])
    @invitation.sender = current_user
    if @invitation.save
      if signed_in?
        InvitationMailer.invitation(@invitation).deliver
        flash[:notice] = "Thank you, invitation sent."
        redirect_to current_user
      else
        flash[:notice] = "Thank you, we will notify when we are ready."
        redirect_to root_url
      end
    else
      render :action => 'new'
    end
  end
end

in invitation_mailer.rb file

class InvitationMailer < ActionMailer::Base
  default from: "holler@thesite.com", content_type: "text/html"

  def invitation(invitation)
    mail to: invitation.recipient_email, subject: "Invitation"
    @invitation_link = invited_url(invitation.token)
    invitation.update_attribute(:sent_at, Time.now)
  end
end

views/invitation_mailer/invitation.text.erb

You are invited to join the site!

<%= @invitation_link %> # INSTANCE VARIABLE THAT IS NIL IN VIEW

routes.rb (only showing relevant line)

match '/invited/:invitation_token', to:  'users#new_invitee', as: 'invited'
Community
  • 1
  • 1

2 Answers2

3

try this way

This is your InvitationMailer

def invitation(invitation)
  @invitation = invitation
  mail(:to => @invitation.recipient_email, :subject => "Invitation")
end

now, in your InvitationsController

if signed_in?
  @invitation.update_attribute(:sent_at, Time.now)
  InvitationMailer.invitation(@invitation).deliver
  ...
else
  ...
end

now, views/invitation_mailer/invitation.text.erb

You are invited to join the site!
<%= invited_url(@invitation.token) %> # INSTANCE VARIABLE THAT IS NIL IN VIEW
Dipak Panchal
  • 5,996
  • 4
  • 32
  • 68
  • This worked. Also works if @invitation.update_attribute(:sent_at, Time.now) stays in InvitationMailer and is called after the mail method. Looks like they key is declaring the instance variable before calling the mail method? Thanks a ton. – Justin Silverman Nov 09 '12 at 07:05
0

try this...

@invitation_link = invited_url(invitation.token, :host => "localhost:3000")

Pragnesh Chauhan
  • 8,363
  • 9
  • 42
  • 53
Aditya Kapoor
  • 1,542
  • 10
  • 13
  • Still nil in the view. It appears it's pointing to the correct url if I run raise @invitation_link.inspect in ActtionMailer prior to invitation.update_attribute(:sent_at, Time.now). It's still just nil when passed to the view. – Justin Silverman Nov 09 '12 at 06:52
  • in the invitation.text.erb file try this.. <%= invited_url(@invitation.token, :host => "localhost:3000") %> – Aditya Kapoor Nov 09 '12 at 06:59