Search code examples
ruby-on-railssendgridsendgrid-api-v3

ROR: What is causing uninitialized constant


I had to update from Sendgrid V2 to V3. I am using the Sendgrid-ruby gem 5.3.

I am getting this error

NameError (uninitialized constant PasswordController::Email):
app/controllers/password_controller.rb:54:in `send_email' 
May 06 08:57:01 burro-staging app/web.1: ArgumentError (wrong number of arguments (given 1, expected 0)):

Here is the line causing the issue (2nd line below).

  mail = SendGrid::Mail.new
  mail.from = Email.new(email: 'no-reply@getburro.com') <-----

Solution

  • Ruby is looking for Email class and couldn't find it. The reason is because Email belongs to Sendgrid module, and should be scoped like so:

    Sendgrid::Email.new ...

    as can be seen here:

    https://github.com/sendgrid/sendgrid-ruby/blob/9dd0cf6c9eb7ecc1e4fe2824f9638468ab5fc818/lib/sendgrid/helpers/mail/email.rb

    module SendGrid
      class Email
    
        attr_accessor :email, :name
    
        def initialize(email: nil, name: nil)
    
    ...
    

    And from the docs: https://github.com/sendgrid/sendgrid-ruby#with-mail-helper-class