无法将模型实例变量传递给邮件程序

时间:2013-09-02 17:44:00

标签: ruby-on-rails-3 ruby-on-rails-3.2

我有一个模型,通过解析CSV文件的名称和电子邮件,通过电子邮件发送邀请。我有一个before_create创建一个url并将其保存为实例变量。创建记录后,应该将结果与URL的实例变量一起发送到邮件程序。似乎URL没有发送到邮件程序,因为电子邮件已成功发送,但使用URL。以下是相关的代码行。我确认正在创建invite_token,因此这不是问题。

注意:我使用SmarterCSV gem解析csv并使用delayed_jobs gem创建后台进程。

让我解释一下这个过程:

Controller(未显示)接收CSV并将其发送到Invitation.import。解析文件,在创建记录之前,创建一个邀请令牌,然后构建一个URL。然后发送电子邮件。

谢谢!

Controller:invitations_controller.rb

类InvitationsController< ApplicationController的     before_filter:get_event#,仅限:[:create]

def new
    @invitation = @event.invitations.new
end

def create
    @invitation = @event.invitations.build(params[:invitation])
    @invitation.event_id = params[:event_id]

    if @invitation.save
        flash[:success] = "Invitation confirmed!"
        render 'static_pages/home'
    else
        render 'new'
    end
end

def import_csv
    @invitation = @event.invitations.new
end

def import
    Invitation.import(params[:file], params[:event_id])
    flash[:success] = "Invitations sent!"
    redirect_to @event
end

  private

     def get_event
    @event = Event.find(params[:event_id])
     end
end

型号:Invitation.rb

class Invitation < ActiveRecord::Base
       before_save { |user| user.email = user.email.downcase }
       before_create :create_invite_token
       before_create :build_url

@@url = "" #added 9/8/13

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

邀请电子邮件:

<!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>

1 个答案:

答案 0 :(得分:0)

我用不同的方法解决了这个问题。我在邀请模型中创建了一个方法,将记录的信息和URL发送给邮件程序。然后我添加了一个after_create回调函数,以便在创建记录后发送电子邮件。请参阅下面的模型更改。

class Invitation < ActiveRecord::Base
   before_save { |user| user.email = user.email.downcase }
   before_create :create_invite_token
   before_create :build_url
   after_create :send_invitation


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

def send_invitation
   if self.accepted == false
      UserMailer.delay.invitation_email(self, @url)
   end
end

private

  def create_invite_token
    self.invite_token = SecureRandom.urlsafe_base64
  end
end