counterbeing
counterbeing

Reputation: 2791

How do I Make a Constant from a Gem Available in Controller

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?

Upvotes: 1

Views: 488

Answers (1)

jstim
jstim

Reputation: 2432

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.

Upvotes: 1

Related Questions