rails no such file to load -- ap (LoadError) - ruby-on-rails

When I try any of the rails, rake command in staging server I got no such file to load -- ap (LoadError),
for rails s:
/usr/local/lib/ruby/gems/1.8/gems/bundler-1.0.22/lib/bundler/runtime.rb:68:in`require': no such file to load -- ap (LoadError)
xxxx
/config/application.rb:7
for rake -T:
rake aborted!
no such file to load -- ap
my application.rb file:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# If you have a Gemfile, require the gems listed there, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env) if defined?(Bundler)
my rails and ruby version:
rails version in local and staging: 3.0.11
ruby version in staging: ruby 1.8.7 (2012-02-08 patchlevel 358)
ruby version in local:ruby 1.8.7 (2011-06-30 patchlevel 352)
But I can run rails s, rails c, rake log:clear everything in my local machine. Gemfile file in staging is same as Gemfile in local.
what am I missing here? how can I resolve this?

In your Gemfile, change this line:
gem "awesome_print", :require => "ap"
...to this:
gem "awesome_print"
I just had the same thing happen to me. ap is a custom defined alias for awesome_print. This allows you to do things like ap Product.first and it will pretty print it for you. Much easier than typing out awesome_print Product.first
I'm not sure why, but when this happened to me, I dropped the alias, and it loaded without an issue. There is probably something else in play here that someone smarter than myself can explain, but I know that this worked for me.

Related

Why does Bundler.require not require dependencies?

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.

Rake cannot load such file cucumber

I'm trying to setup an application on a ubuntu VPS with rvm installed. The thin webserver works fine, but all the rake commands fails with same error message.
LoadError: cannot load such file -- cucumber/rake/task
I've this on my Rakefile:
require 'cucumber/rake/task'
I've tried with
gem install cucumber
gem install guard-cucumber
gem install cucumber-rails
gem install rake
bundle exec rake db:migrate
Nothing seems to work
#which ruby
/home/ubuntu/.rvm/rubies/ruby-2.0.0-p353/bin/ruby
#which rake
/home/ubuntu/.rvm/gems/ruby-2.0.0-p353#search-demo/bin/rake
What else could be or how I can debug this?
You can check the load path printing the $: var. check if you have the cucumber gem there and if it match the version that you have on your local machine, where I suppose is working.
If the gem path is not there, be sure it is installed with
gem list | grep cucu
Also compare versions with your local environment.
Check your Gemfile, make sure you dont have those gems on a group :development block or similar, cause you have probably set RAILS_ENV in your vps to production.

Why does Ruby run only some tests and not others?

I'm using this command to run some tests...
bundle exec ruby -Itest test/functional/*.rb
In my test/functional dir I have two files...
file_sets_controller_test.rb
user_sessions_controller_test.rb
With the above command, the tests in file_sets_controller_test.rb all run but the ones in user_sessions_controller_test.rb don't run at all -- no errors or other output is reported.
However, I can run that file directly no problem, with this...
bundle exec ruby -Itest test/functional/user_sessions_controller_test.rb
That works fine.
I know that another option is to use rake test functionals, but that is extremely slow compared to running them directly.
ruby 1.9.3p327 (2012-11-10 revision 37606) [x86_64-darwin11.4.2]
Rails 3.2.12
Here's a part of my Gemfile...
group :development, :test do
gem 'ansi'
gem 'turn'
gem 'minitest'
gem 'minitest-matchers'
end
And here's my test_helper.rb...
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'turn/autorun'
Turn.config.ansi = true
require 'minitest/autorun'
class ActiveSupport::TestCase
fixtures :all
end
Removing the Turn and Minitest gems doesn't change anything as far as I can tell.
The ruby command takes a ruby file to run as its first argument and makes additional arguments available to the ruby program. The shell is expanding your glob expression into 2 arguments and passing them to ruby, so ruby is running the first file name in the expansion.
Additional:
I think you can do what you want with something like...
bundle exec ruby -Itest -e "Dir.glob('test/functional/*_test.rb').each{|f| require File.expand_path(f)}"

"uninitialized constant Gem" or "Rails" when starting Rails 2 app

I have an application using Rails 2.3.4 which I'm trying to run. No matter how I start it, by using script/console or script/ferret_server, it always boils down to the same error:
uninitialized constant Gem
When I search online, I only find people who has a similar problem finding something inside of the Gem class. But in my case, Gem is just not there at all.
I am running Ruby on Ubuntu. I tried using the version of Ruby that came with APT and a more recent one with RVM. Nothing helped.
Any idea how what can be causing thing and how to fix it? Thanks in advance!
Some extra details, as requested by commenters:
Output of ruby -v:
ruby 1.8.7 (2011-02-18 patchlevel 334) [x86_64-linux], MBARI 0x6770, Ruby Enterprise Edition 2011.03
Output of gem -v:
1.8.6
Calling ruby script/console outputs a similar error (edited):
Loading development environment (Rails 2.3.4)
/var/www/sites/example.com/releases/20110726061310/config/environment.rb:12:NameError: uninitialized constant Gem
/usr/local/rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/irb/init.rb:254:in `require': no such file to load -- console_app (LoadError)
from /usr/local/rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/irb/init.rb:254:in `load_modules'
from /usr/local/rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/irb/init.rb:252:in `each'
from /usr/local/rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/irb/init.rb:252:in `load_modules'
from /usr/local/rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/irb/init.rb:21:in `setup'
from /usr/local/rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/irb.rb:54:in `start'
from /usr/local/rvm/rubies/ree-1.8.7-2011.03/bin/irb:17
I'm trying to call Ferret Server to allow Passenger to connect to it, with the command:
ruby --debug script/ferret_server -e production start
Outputs the following error:
Exception `NameError' at ./script/../vendor/plugins/acts_as_ferret/lib/../../../../config/environment.rb:12 - uninitialized constant Gem
uninitialized constant Gem
Alternatively, calling bundle exec ruby --debug script/ferret_server -e production start outputs the following error:
superclass mismatch for class GemDependency
Please note that I added the following right above the Rails::Initializer.run do |config| line in config/environment.rb:
require 'thread'
if Gem::VERSION >= "1.3.6"
module Rails
class GemDependency
def requirement
r = super
(r == Gem::Requirement.default) ? nil : r
end
end
end
end
require File.join(File.dirname(__FILE__), 'boot')
Remove the lines mentioned above gives uninitialized constant Rails errors instead.
Try putting:
require 'rubygems'
Just after require 'thread'

How to avoid 'no such file to load -- map_by_method' script/console with bundler?

I am running rails 2.3.10 with bundler. I use rvm, in global gemsets usually I put the gems useful for the irb. By default I had these there: map_by_method, what_methods, ap, net-http-spy, hirb, looksee, pp, wirble.
When I start irb, the gems it needs are avalaible for it, and the things look as they should. However, when I start script/console, it cannot reach the gems in the current rvm gemset only those which are defined in Gemfile. I get message no such file to load -- map_by_method. Obviously I do not want to put those irb specific files to my Gemfile.
Do you know some way, how to convince script/console to use also gems outside bundler?
bundler will never use any gem that is not defined in the Gemfile. If those gems are for development use only consider adding them to the Gemfile in a group called :development
You can hand-require gems from within script console if you need them:
$ script/console
=> require 'rubygems'
=> require 'mygem'
=> include 'MyGem::Stuff'

Resources