Question

I'm very new to ruby and rails (3 days and counting), so my problem is probably something stupid. However, it seems to be something stupid that couldn't be resolved by searching for answers online. :(

I'm creating a simple blog app following this guide: http://guides.rubyonrails.org/getting_started.html. It works fine, no issues.

Then I set up SendGrid and I'm able to send emails through it just fine as well.

Now, I'm trying to use this sendgrid gem: https://github.com/stephenb/sendgrid. I installed it using 'gem install sendgrid' and it seemed to work without problems.

According to the instructions on github, I just need to add "include SendGrid" in my mailer class and I'm good to go. I did just that:

class Emailer < ActionMailer::Base
    include SendGrid
    ...
end

But when I run the app, I get this error: uninitialized constant Emailer::SendGrid

I did a couple of other things that seemed to make sense based on what I've read so far:

  • Added 'gem sendgrid' in my Gemfile. This added three lines to my Gemfile.lock:
    • sendgrid (1.0.1)
    • json
    • json
  • Added 'require sendgrid' in my environment.rb file.

Yet, the error still persists. One thing that might be indicative of a problem is that when I look at the $LOAD_PATH, it doesn't have the sendgrid directory. For comparison, another gem included in the same manner is sqlite3 and I see the ".../sqlite3-1.3.4/lib" path there, but I don't see ".../sendgrid-1.0.1/lib".

Can somebody discern what kind of stupidity has afflicted me this time?

EDIT:

I discovered something very interesting. For me at least... If I go into the rails console, things actually seem to work fine. Here is the output of my session:

ruby-1.9.2-p290 :006 > include SendGrid
 => Object 
ruby-1.9.2-p290 :007 > sendgrid_category :use_subject_lines
 => :use_subject_lines 
ruby-1.9.2-p290 :008 > sendgrid_category "Welcome"
 => "Welcome" 
ruby-1.9.2-p290 :009 > p = Post.new(:title => "A new post", :content => "With garbage text")
 => #<Post id: nil, name: nil, title: "A new post", content: "With garbage text", created_at: nil, updated_at: nil> 
ruby-1.9.2-p290 :010 > Emailer.send_email("nick@sidebark.com", p).deliver
 => #<Mail::Message:2194904560, Multipart: false, Headers: <Date: Thu, 22 Sep 2011 16:52:41 -0700>, <From: ... blah, bah, blah...>>

The email got sent AND the category got registered by SendGrid (I could see it on the Statistics page).

So, the big question is: Why is it that my app only allows me to include SendGrid when I'm running commands form the console? What's the difference in environment, etc.?

Also note that the emails get sent form the console, but NOT from the app flow, even though the development.log says that an email was sent in both situations...

Was it helpful?

Solution

For anyone who didn't read the comments on the original post, the answer is that the server needs to be restarted once you make changes to the dependencies or the configuration of your app.

As far as the reason things were working in the console, every time you load up a Rails console, you're reloading your entire app including the new dependencies and config files.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top