1

I have a feeling this is a dumb one, but I've spent some time trying to figure it out and googling it, and to no avail. I'm trying to use a Ruby Gem in a Controller. I have included it in my Gemfile, run bundle install, seen it show up in my gems list, restarted my local server. But somehow whenever I try to call the gem ( rails_rrdtool ) It just tells me

uninitialized constant RrdgraphsController::RRD
app/controllers/rrdgraphs_controller.rb:22:in `show'

The spot in my code where it wigs is when I'm calling

RRD.graph

It's as though it doesn't know where the heck the gem is... However, I can use require to import it successfully into an irb session. So I know that it works, it's just not getting in there some how...

Bundler should be handling the inclusion of the gem I assume. Am I calling it in the wrong place?

counterbeing
  • 2,721
  • 2
  • 27
  • 47
  • what gem is it? you need to prefix the constant with the right class. right now it thinks it is defined inside of the RrdgraphsController class. It's a namespace issue, I think. – jstim May 22 '12 at 00:01
  • 1
    Nevermind on the comment above, I didn't realize that it was the gem name as well. I thought it was just a constant defined inside of the gem. What about prefixing it with :: to try to get it to look outside the current class? `::RRD.graph` – jstim May 22 '12 at 00:11
  • @jstim Whoo hoo! That appears to have done it :) I was wondering if it needed some kind of prefix, but didn't think it would work with just :: If you submit it as an answer I'll give you a stamp of approval! Thanks! – counterbeing May 22 '12 at 00:28
  • I wonder if Ruby got confused because the class name is all upper case. If it was a class called `Rrd` I wonder if it would still freak out. – jstim May 22 '12 at 00:30

1 Answers1

1

This looks like a namespacing issue. Your error says it is looking for the constant inside of the current class: RrdgraphsController::RRD when it should be looking for a class outside of the current context.

Try prefixing the class name with a double colon to fully define the location of the class.

::RRD.graph #rest of your code

There's a good analogy of what this does in this other accepted answer. Basically it creates an absolute path so Ruby doesn't have to guess.

Community
  • 1
  • 1
jstim
  • 2,432
  • 1
  • 21
  • 28