Djjhjuser10735674
Djjhjuser10735674

Reputation: 27

Action Mailer 'undefined method `[]' for nil:NilClass'

Im getting an error when trying to send an email. Not to sure why but here is my code in my controller and mailer

Here is my controller code below

class Invitation::InvitesController < ApplicationController

def invite_provider
      @patient = Patient.find_by_id(invite_params[:invitable_id])
      recipient = params[:email]
      InviteMailer.provider_invite(recipient).deliver_now
      flash[:success] = "An email has been sent to"
      redirect_back(fallback_location: root_path)
    end

end

Here is my mailer code

class InviteMailer < ApplicationMailer
def provider_invite(recipient)
    @recipient = recipient
    mail(
      to: recipient[:email],
      subject: I18n.t('provider_invite_subject')
    )
  end
end

Upvotes: 0

Views: 1374

Answers (2)

ray
ray

Reputation: 5552

You have called InviteMailer.provider_invite(recipient) where recipient is email inside controller.

You can change in controller,

InviteMailer.provider_invite(email: recipient)

and then,

class InviteMailer < ApplicationMailer
  def provider_invite(attr)
    @recipient = attr[:email]
    mail(
      to: @recipient,
      subject: I18n.t('provider_invite_subject')
    )
  end
end

And error is due to your params o not have email key, so recipient is nil passed through controller

Upvotes: 0

Ursus
Ursus

Reputation: 30056

At the call to mail, in the to option, you're basically sending params[:email][:email]. I don't think that's what you want.

recipient = params[:email]

and then

to: recipient[:email],

Upvotes: 1

Related Questions