3

I've included the yajl gem in my Gemfile via gem 'yajl-ruby', '~> 1.1.0' however upon calling parser = Yajl::Parser.new in my store controller I receive the error uninitialized constant StoreController::Yajl

If I execute require 'yajl' at the beginning of the controller all is well but I was under the impression that the gem should be available to controllers within the application simply by including in the gem file.

rudolph9
  • 8,021
  • 9
  • 50
  • 80
  • Did you `require` inside the controller class or at the top of the file? Try using a fully-qualified reference: `parser = ::Yajl::Parser.new` – Matheus Moreira Jan 26 '12 at 22:39
  • I did require it inside the controller class and it works fine but I was under the impression that including it within the `Gemfile` was enough to make it available to all classes within the rails application? – rudolph9 Jan 30 '12 at 04:12
  • Yeah, `Bundler` ought to take care of it for you. Is there a `Bundler.require` call in your `config/application.rb`? Have you run `bundle install`? – Matheus Moreira Jan 31 '12 at 17:35
  • There is a call `if defined?(Bundler)¬ # If you precompile assets before deploying to production, use this line¬ Bundler.require(*Rails.groups(:assets => %w(development test)))¬ # If you want your assets lazily compiled in production, use this line¬ # Bundler.require(:default, :assets, Rails.env)¬ end¬` I have run bundle install but to no avail. – rudolph9 Feb 01 '12 at 00:30

1 Answers1

3

You have to indicate to Bundler the main file of the gem with the require option:

gem 'yajl-ruby', '~> 1.1.0', require: 'yajl'

This is necessary for gems whose name is different that their main file name.

See the Gemfile manual.

rudolph9
  • 8,021
  • 9
  • 50
  • 80
Florent2
  • 3,463
  • 3
  • 28
  • 38