我正在尝试将一个mailer集成到我的应用程序中。下新订单时,我需要发送一封事务性电子邮件。在将收件人传递给邮件程序时,我不断遇到无方法错误。当我明确定义收件人时,这很好,但我需要将模型中的收件人变量传递给消息,以获得我想要的行为。任何调试方面的帮助都将不胜感激,因为我似乎无法找到我的错误。
错误输出:
NoMethodError (undefined method `recipient' for nil:NilClass):
app/mailers/bed_mailer.rb:8:in `bed_email
邮递员:
class BedMailer < ApplicationMailer
default :from => 'info@paperlesspcs.com'
def bed_email(bed)
@bed = bed
mail(
:subject => 'Critical Documentation Needed' ,
:to => @bed.recipient ,
:track_opens => 'true',
:body => 'something'
)
end
end
相关控制器操作:
def create
@bed = current_supplier.beds.build(bed_params)
respond_to do |format|
if @bed.save
BedMailer.bed_email(@beds).deliver_later
format.html { redirect_to @bed, notice: 'Bed was successfully created.' }
format.json { render :show, status: :created, location: @bed }
else
format.html { render :new }
format.json { render json: @bed.errors, status: :unprocessable_entity }
end
end
编辑/更新
我纠正了下面评论中提到的打字错误,但现在当我删除:body
行以转发到我喜欢的邮件布局Missing template bed_mailer/bed_email with "mailer". Searched in: * "bed_mailer"
时,遇到了无模板错误
我的预期布局:
bed_mailer.html.erb
<h2> Critical Documentation Needed: </h2>
<p> In order to quickly process your referral for <strong> <%= @bed.patient_name %> </strong>, we need additional documentation.
<br>
For your convience, please follow this link to electronically complete the documentation process: </p>
<br>
<h4> <%= link_to 'Click Here to complete the necessary documents', edit_bed_url(:id => @bed.id, :token => @bed.token) %> </h4>
<br>
我相信您的控制器操作中有一个拼写错误。
您应该用@bed
替换@beds