Rails Mailer没有解雇的问题

时间:2017-07-22 15:03:11

标签: ruby-on-rails model-view-controller actionmailer

我有一个agent模型和一个agent_card模型。有人在招聘时会为他们创建代理卡。我试图让一个邮件发送一旦该人被雇用,但我不知道为什么它不工作。任何帮助表示赞赏。

代理商控制器 - 雇佣方法:

def employ
  @agent.employee = true
  @agent.on_probation = true
  @agent.save(validate: false)
  AgentCard.create(agent_id: @agent.id, hire_date: Date.current)
  AgentCardMailer.agent_card_message(@agent_card, @agent).deliver_later
  respond_to do |format|
    format.html { redirect_to :back }
    format.json { head :no_content }
  end
end

agent_card_mailer.rb:

class AgentCardMailer < ActionMailer::Base
  default from: "help@mail.com"

  def agent_card_message(agent_card, agent)
    @agent_card = agent_card
    @agent = agent

    mail(:to => "mike@mail.com", :subject => "New Agent Card Created!")
  end
end

agent_card.html.erb:

Hiya Mike,<br><br>

A new Agent Card has been created!<br><br><br>

Name: <%= @agent_card.agent.name %><br><br>
Date Created: <%= @agent_card.created_at.strftime("%m/%d/%Y @ %l:%M %p") %>

错误:

2017-07-22T14:57:19.132Z 1614 TID-ox63wsdb0 WARN: {"class"=>"ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper", "wrapped"=>"ActionMailer::DeliveryJob", "queue"=>"mailers", "args"=>[{"job_class"=>"ActionMailer::DeliveryJob", "job_id"=>"c72ff671-6834-4274-9668-664305ff4df2", "queue_name"=>"mailers", "arguments"=>["AgentCardMailer", "agent_card_message", "deliver_now", nil, {"_aj_globalid"=>"gid://nooklyn/Agent/61967"}], "locale"=>"en"}], "retry"=>true, "jid"=>"a8ea332f8212f34f6ca1aedf", "created_at"=>1500735186.9071932, "enqueued_at"=>1500735439.0713248, "error_message"=>"undefined method `agent' for nil:NilClass", "error_class"=>"ActionView::Template::Error", "failed_at"=>1500735187.086685, "retry_count"=>4, "retried_at"=>1500735439.130213}

1 个答案:

答案 0 :(得分:0)

在视图中,我没有给代理人打电话。

需要:

Name: <%= @agent.agent_card.agent.name %><br><br>
Date Created: <%= @agent.agent_card.created_at.strftime("%m/%d/%Y @ %l:%M %p") %>

周六编程...... amirite?!