Rails Mailer 的问题访问数据

Issue accessing data for Rails Mailer

在我添加 rental_application_personal_details 并尝试从 table 访问 first_name 后问题开始了。

控制器:

def create
  unless @rental_application.submittable?
    redirect_to @rental_application, notice: "You can't submit the rental application unless you complete it!"
    return
  end

  if @rental_application.submitted!
    RentalApplicationMailer.sales_agent_message(@rental_application, @rental_application_personal_details, @agent).deliver_now
    if @rental_application.payment_request.authorized
    CapturePaymentJob.perform_later(@rental_application.payment_request)
    else
      @rental_application.payment_request.void!
    end

    redirect_to @rental_application, notice: 'Your application was submitted! :)'
  else
    redirect_to @rental_application, notice: 'We were unable to submit your application.'
  end
end

租赁申请邮寄者:

def sales_agent_message(rental_application, rental_application_personal_details, agent)
  @rental_application = rental_application
  @rental_application_personal_details = rental_application_personal_details
  @agent = agent
  @sales_agent = @rental_application.sales_agent.email
  mail(:to => @sales_agent, :subject => "New Rental Application Recieved!")
end

销售代理留言:

嗨!

<%= @rental_application_personal_details.first_name %> 已向您发送租赁申请! %>

查看 <%= link_to 'HERE!', matrix_rental_applications_url %>

错误:

@rental_application_personal_details 似乎永远不会在任何地方设置。在您的 create 方法中,您正在使用它,但您从未首先给它赋值。因此,它是零。 nil.first_name 是您当前遇到的错误。

你可以写成

if @rental_application.submitted! && @rental_application_personal_details.present?

//code to send the mail

end