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?

4
  • 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. Commented May 22, 2012 at 0: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 Commented May 22, 2012 at 0: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! Commented May 22, 2012 at 0: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. Commented May 22, 2012 at 0:30

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.