In my Gemfile I have:
gem 'addressable'
In search_controller.rb:
uri = Addressable::URI.new
uri.query_values = {:q => query}
I get this error:
NameError (uninitialized constant SearchController::Addressable):
If I put
require 'addressable/uri'
on top of my controller, it works!!. I have already done "sudo bundle install" and it shows addressable is installed. What am I doing wrong?
Looking at addressable gem source I see it has no lib/addressable.rb which is default file which rubygems or bundler require when loading required gem. So it looks like it is designed this way on purpose - to make you explicitly require only the libraries you need.
Related
I want to use awesome print without putting it in my rails 5 app. Just in the console. The documentation for requiring it in irb is not working.
That's because bundler isolates the gems available to load to what's in your Gemfile.
The best way to get around this is to add the gem to your Gemfile
gem 'awesome_print', require: false, group: :development
And in your .irbrc, you can require it, so that it is only enabled for you:
begin
require 'awesome_print'
rescue LoadError => err
warn "could not require awesome_print: #{err}"
end
However, if you aren't permitted to add awesome_print to your repository for whatever reason, there are a few hacks to get gems installed, but not in your Gemfile to load in this GitHub Gist.
One such example that could be placed at the top of your .irbrc:
# Add all gems in the global gemset to the $LOAD_PATH so they can be used even
# in places like 'rails console'.
if defined?(::Bundler)
global_gemset = ENV['GEM_PATH'].split(':').grep(/ruby.*#global/).first
$LOAD_PATH.concat(Dir.glob("#{global_gemset}/gems/*/lib")) if
global_gemset
end
cd your/rails/project
irb
inside irb, run:
require 'awesome_print'
require './config/environment'
and you have both rails console and awesome_print gem while the gem is installed outside of the bundler.
I am developing a gem at the moment. Here's how the .gemspec looks like:
gem.add_dependency 'activerecord', '~> 4.2'
...
gem.add_development_dependency 'rails', '4.2.5'
...
and here's my Gemfile:
source 'https://rubygems.org'
gemspec
I am setting up my main file, lib/my_gem.rb like so:
module MyGem
module Lib
end
end
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
Bundler.require
However, if I start bundle console from my gem's folder, the dependencies are not required:
$ bundle console
Resolving dependencies...
irb(main):001:0> Rails
NameError: uninitialized constant Rails
...
irb(main):002:0> ActiveRecord
NameError: uninitialized constant ActiveRecord
...
What am I doing wrong?
I believe dependencies included via the gemspec command in the Gemfile are not automatically required by Bundler.require. Only gems listed directly in the Gemfile itself are.
Additionally, gems included in only certain Bundler groups, like 'development', may not be required by a Bundle.require even if they were included directly in the Gemfile, you need to tell bundler to require groups other than default.
You can always require gems manually though, like require 'rails'. Bundle.require doesn't do anything but require gem for each gem in your Gemfile (or at least default group in your Gemfile), it's doing any magic other than looking up all the gems in your Gemfile and requiring them. Bundle.require is considered by some to be a bad practice anyway, you should just require the dependencies you need in the files you need them, some say. Although Rails doesn't agree, and Rails apps have their own somewhat complicated way of auto-loading things.
But if you are in a Rails app, as your example dependencies suggest... why are you doing any require 'bundler/setup' or Bundle.require yourself at all though, instead of letting Rails boot process take care of it? Rails boot process will take care of requiring the Bundler groups you probably expect too (like the "development" group when in Rails.env == 'development').
You can use the bundler api yourself directly like you're doing, it's not too hard. But Rails ordinarily takes care of this for you, and if you are using Rails, rails probably already has done a Bundler.setup and Bundler.require as part of Rails boot process.
I'm trying to use the barby gem that I installed, but when I do it gives me this error.
LoadError cannot load such file -- barby
Here's the require methods in my controller.
require 'barby'
require 'barby/barcode/ean_13'
require 'barby/outputter/ascii_outputter'
require 'barby/outputter/html_outputter'
class PalletsController < ApplicationController
-snip-
end
Here's the gem in my gemfile.
gem 'barby'
Any help would be greatly appreciated.
You only add require when you are dealing with a ruby project. In your case(ruby-on-rails), you can remove those lines.
Also, keep in mind that you should add your gems into your Gemfile
bundle install? It sounds like you do not have the gem installed when ruby tries to load it, it can not find it. Gem list will list out the installed gems on the system, or bundle exec gem list.
gem list
I had to fork the gem Thor, coz my cli has one command run which is reserved in the Thor lib itself, changed its name to millisami-thor just in the .gemspec as follows:
Gem::Specification.new do |s|
...
s.name = 'millisami-thor'
...
end
and to use it, I pushed the gem under the name millisami-thor to rubygems.org and in the Gemfile of my cli project, I put gem 'millisami-thor', :require => 'thor'
Now while testing bundle exec cucumber features or to try out the executable, just did ./bin/executable --params and it worked out.
Now, I build the gem with gem build gemname.gemspec that generates gemname.gem and installed with gem install gemname.gem and it gets installed as well as the binary too. Fine, till here.
Now, when I use the binary cmd like executable --params, it looks for the original thor library instead of the forked one.
I figured out that this was due to the require ... in the executable.
require 'thor'
require 'fileutils'
require 'gemname/cli'
Cf::CLI.start
coz in there I explicitly required the original thor.
Now, when I change it to require 'millisami-thor, it cannot find and says:
...
custom_require.rb:36:in `require': no such file to load -- millisami-thor (LoadError)
...
In the Gemfile, I could have done gem 'millisami-thor, :require => 'thor' so that it loads the forked gem.
But how can I do the same if its just the require 'millisami-thor' ?
The only option I can think of is to change all the class names to 'MillisamiThor' instead of 'Thor' and the file names too. But this will be too messy and ugly.
I could have spotted this if I had installed my gem and test it before. But I did just in the test environment, in which the bundler requires the millisami-thor's thor file, so I didn't have this problem till today.
Is there any other way out to achieve this without any messy hacks?
You can keep the original gem setup and point Bundler to your git repository fork:
gem 'thor', :git => 'git://github.com/yourname/thor.git', :require => 'thor'
or even a local path
gem 'thor', :path => '/path/to/thor.git', :require => 'thor'
I'm having an issue with using RedCloth in my local application. I keep getting the following error:
uninitialized constant ActionView::CompiledTemplates::RedCloth
This happens because I have the following code:
<%= RedCloth.new("Some text").to_html %>
What I tried to do is put this in the environment.rb file:
require "RedCloth"
However, when I do this, I get a huge error with my Phusion Passenger + Nginx configuration, which I've detailed in the following forum: http://railsforum.com/viewtopic.php?id=42560
Any help would be great. Thanks!
Make sure your Gemfile has a gem 'RedCloth' in it. Regardless of which gems are actually installed in your system, Rails will only use the gems listed in your Gemfile. You do not need the require "RedCloth" statement either.
I had exactly the same error and gem 'RedCloth' line was present in Gemfile. What helped was adding require statement in the beginning of controller file
require 'redcloth'
class StaticController < ApplicationController
...