0

I'm trying to integrate a mailer into my application. I need to send a transactional e-mail when a new order is placed. I keep running into a no method error when passing the recipient to the mailer. This works fine when I define the recipient explicitly, but I need to pass the recipient variable from my model to the message in order to get the behavior I desire. Any help debugging would be greatly appreciated, as I can't seem to track down my mistake.

Error output:

NoMethodError (undefined method `recipient' for nil:NilClass):
  app/mailers/bed_mailer.rb:8:in `bed_email

Mailer:

  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

Associated Controller Action:

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

Edit/Update

I corrected the typo error mentioned in the comment below, but now running into a no template error when I remove the :body line to forward to my preferred mailer layout Missing template bed_mailer/bed_email with "mailer". Searched in: * "bed_mailer"

My intended layout:

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>
Community
  • 1
  • 1
PSCampbell
  • 858
  • 9
  • 27

1 Answers1

0

I believe you have a typo in your Controller action.

You should replace @beds with @bed

yez
  • 2,368
  • 9
  • 15