1

I am using Ruby 1.9.3 and getting the following error on the following line while trying to use the SendGrid API.

ERROR [on this line below "mailer.mail(mail_defaults)"]:

 NoMethodError (undefined method `to_h' for #<Hash:0x00000005da0958>): 

CODE:

assuming some users

      recipients = []

        recipient = SendGrid::Recipient.new('jn@geo.com')
        recipient.add_substitution('name', 'Billy Bob')
        recipient.add_substitution('number', 1234)
        recipient.add_substitution('time', '10:30pm')

        recipients << recipient



      # then initialize a template
      template = SendGrid::Template.new('blahblah7bef2-d25b00')

      # create the client
      # see client above

      # set some defaults
      mail_defaults = {
        :from => 'no-reply@geo.com',
        :html => '<h1>I like email tests</h1>',
        :text => 'I like email tests',
        :subject =>'Test Email is great',
      }

      mailer = SendGrid::TemplateMailer.new(client, template, recipients)

      # then mail the whole thing at once
      mailer.mail(mail_defaults)

I thought it might be my mail_defaults array so I tried this (see below) and received the same error on the same line.

mail_defaults = {
        from: 'no-reply@geo.com',
        html: '<h1>I like email tests</h1>',
        text: 'I like email tests',
        subject: 'Test Email is great',
      }

Do I have an error in my code or is their an error in SendGrids mailer.mail method?

jdog
  • 10,351
  • 29
  • 90
  • 165
  • Can you please show us the complete back trace? You might need to remove backtrace silencers in your `config/initializers/backtrace_silencers.rb`. – Matouš Borák Mar 29 '16 at 05:53

1 Answers1

0

This problem is with mail.mailer. This uses the to_h method internally, which was implemented in Ruby 2.1. Effectively, the sendgrid sdk requires ruby 2.1 now. I had the same issue with Ruby 1.9.3 and solved it by upgrading to 2.1.

This is what I used to upgrade to Ruby 2.1 on CentOS 6 (not my gist): https://gist.github.com/mustafaturan/8290150

Hope this helps.

NullPointer
  • 545
  • 1
  • 6
  • 17