Gem available in irb but not rails console - ruby-on-rails

I am trying to use the RedCloth gem in my rails project. When I used irb I can load the gem:
require 'rubygems'
require 'RedCloth'
and it works fine, but when I try the same thing in the rails console I get an error message stating that the gem cannot be found.
Does anyone have any idea what might cause this?

You can append gem path to ruby load path. Do this:
gem which faker
=> /usr/local/ruby/......../faker-0.1.1/lib/faker.rb
Start Rails console and do the following:
$: << '/usr/local/ruby/......../faker-0.1.1/lib/'
and now load faker gem
require 'faker'
=> true

Does your rails project's Gemfile include gem 'RedCloth' in it? Rails will only load the gems specified in that file.

Related

How do I get awesome_print to work without using ~/.irbrc file?

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.

Require 'barby' not working. - cannot load such file -- barby

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

"Cannot load such file" error when deploying Rails app to Heroku

I'm using a gem for gmail in my Rails app. My Gemfile contains:
gem 'gmail-api-ruby', :require => 'Gmail'
And in my controller, I initialize the gem with (using devise/omniauth to get the refresh_token from Google):
Gmail.client_id = ENV['CLIENT_ID']
Gmail.client_secret = ENV['CLIENT_SECRET']
Gmail.refresh_token = current_user.refresh_token
This works fine in development, but when I deploy to Heroku, I get the following error:
/app/vendor/bundle/ruby/2.2.0/gems/bundler-1.7.12/lib/bundler/runtime.rb:76:in `require': cannot load such file -- Gmail (LoadError)
I cannot figure out why. Should I be requiring the gem somewhere else in my app?
Ruby 2.2.0
Bundler: 1.8.5
Rails: 4.2.0
require is case sensitive if the underlying filesystem is case sensitive (which it is on linux, which is what underpins heroku)
Change to :require => 'gmail' instead of Gmail and you should be ok.
Just remove useless require option from your Gemfile because bundler can load classes inside of this gem without specify require option in your case.
gem 'gmail-api-ruby', '~> 0.0.10'
and run bundle install.
FYI: read section about require option here.

pry-rails not rails console working with Rails 4.0

included 'pry-rails' in my Gemfile run console and get the following
#marketplace-rails/gems/pry-rails-0.2.0/lib/pry-rails/commands.rb:3:in `block in <module:PryRails>': undefined method `create_command' for #<Pry::CommandSet:0x007fcfaa67a878> (NoMethodError)
Step 1. don't use 'pry-rails' ? (not sure how to get that gem
working)
Step 2. in gemfile include gem 'pry'
Step 3. in application.rb include the following...
console do
require "pry"
config.console = Pry
end
run rails console be happy to have pry back in your life.
For what it's worth, I just tried this:
group :development, :test do
gem 'pry-rails'
end
Followed by a bundle install and a 'rails c' and pry came up.
Try stopping spring,
$ bin/spring stop
worked for me after installing the gem, didn't even have to change it in the config/environment/development file either
Make sure you have pry included in your Gemfile and add
config.console = Pry
to your config/environments/development.rb or to your config/application.rb if you want it in every environment.

Cannot load Gmail gem in console/app, but can in irb

I'm bewildered here. While I can run require "gmail" in irb and successfully load the Gmail gem, doing so in my rails console returns false. I made sure to include all the directories in $LOAD_PATH for irb in the $LOAD_PATH for my console, but still cannot get the gem to load in my console.
It may be that the gmail gem is conflicting with another gem that I have installed, but I don't know how to confirm this. It seems other people have had similar problems:
Why is autoload failing to load files for gems
Ruby autoload conflicts between gmail and parse_resource gems
Rails: Using ruby-gmail gem causes problems
I made sure to include 'gem "gmail"' in my Gemfile and run bundle install. Still no luck!
Stuck here, so would appreciate any help.
false doesn't mean that the gem is failing to load, it means that the gem is already loaded.
It the rails console could not find the gem you would get a LoadError. Here's an example of an app of mine that has gem 'haml' in the Gemfile.
1.9.2p320 :001 > require 'haml'
=> false
1.9.2p320 :002 > require 'foo'
LoadError: no such file to load -- foo
Another way to see this is to require 'gmail' a second time in your irb session.
You must add this line to your Gemfile:
gem 'gmail'
This is because the gems loaded by your application are restricted to just the ones specified inside the Gemfile, and their dependencies (and the dependencies of the dependencies, and so on).

Resources