I've deployed a few rails apps but just infrequently enough to feel like every time is my first time. This time I'm installing a sinatra app.
I'm using mod_passenger and I'm getting a problem with a missing gem. However it's there.
From the error in the browser I see:
no such file to load -- json (LoadError)
./application.rb:10:in `require'
./application.rb:10
config.ru:1:in `require'
config.ru:1
/usr/lib64/ruby/gems/1.8/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
/usr/lib64/ruby/gems/1.8/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
config.ru:1:in `new'
config.ru:1
The top of the file looks like this:
require 'rubygems'
require 'sinatra'
require 'json'
require 'sequel'
require 'sinatra/sequel'
(there are comments above these lines, hence the line number differences)
When I open irb and try to require the gems it works:
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'json'
=> true
Sadly, I'm using RHEL 6.2 which is different enough from Ubuntu that I'm a little uncomfortable. But based on my understanding this should work.
Any ideas on things I could look for to see what could cause my problem?
are you sure that passenger is running the same ruby version like irb?
Related
I am sure this is a simple issue but since I have looked so long I cannot see it. So I am running ruby 1.9.3 with Sinatra, sqlite3, datamapper, dm-sqlite-adapter. When I try to run Sinatra, I get this:
/Users/XXX/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- datamapper (LoadError)
from /Users/XXX/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from app.rb:2:in `<main>'
Here are the first two lines:
require 'sinatra'
require 'datamapper'
The gem is installed. (datamapper (1.2.0))
You need to require data_mapper, not datamapper (note the underscore):
require 'data_mapper'
See the DataMapper getting started page.
When I try to require the tagfile/tagfile which is part of rtaglib I get a LoadError:
$ ruby main.rb
/usr/share/rubygems/rubygems/custom_require.rb:36:in `require': cannot load such file -- tagfile/tagfile (LoadError)
from /usr/share/rubygems/rubygems/custom_require.rb:36:in `require'
from main.rb:4:in `<main>'
I installed rtaglib with
$ gem install rtaglib
Here is the top part of my main.rb:
require 'date'
require 'find'
require 'tagfile/tagfile'
None of the suggestions here (Problem with Ruby + rtaglib gem) work. taglib is installed (1.7.2)
Trying it with other gems, like sinatra, works perfectly. Does anyone know the reason I cannot load rtaglib?
Can't you just require 'tagfile'? I just tried it and it worked fine.
Doing the getting started of Sinatra.
I get this error:
./sinatra.rb:5: undefined method `get' for main:Object (NoMethodError)
from /usr/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
from /usr/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:36:in `require'
from sinatra.rb:3
Googling on these errors returns ruby LoadError: cannot load such file which I don't see how that relates to Sinatra.
Not sure what other info I need to share to make my question clearer. So just tell me what other commands I should run to make the question clear.
UPDATE: Actual code
# sinatra.rb
require 'rubygems'
require 'sinatra'
get '/' do
'hey girl'
end
The problem here is due to you naming your file sinatra.rb. When you run that file, the first thing it does is require 'sinatra', and since the current directory is on the load path in Ruby 1.8.7, it tries to load itself. It then gets to the call to get '/' do ..., but since the real Sinatra hasn’t been loaded this results in the error.
The fix is to rename your file to something other than sinatra.rb, you could use myapp.rb as suggested in page you linked to.
I'm scripting with Ruby 1.9.2dev in Backtrack 5 but I'm having some problems when try to parse html entities with the library "htmlentities".
I cannot load the library although I have installed it with gem.
I'll show you the problems I'm having in the console:
root#bt:~# gem list -d htmlentities
*** LOCAL GEMS ***
htmlentities (4.3.1)
Author: Paul Battley
Homepage: https://github.com/threedaymonk/htmlentities
Installed at: /var/lib/gems/1.9.2
A module for encoding and decoding (X)HTML entities.
root#bt:~# irb irb(main):001:0> require 'htmlentities' LoadError: no such file to load -- htmlentities
from (irb):1:in `require'
from (irb):1
from /usr/bin/irb:12:in `<main>'
This is the same problem I'm having with nokogiri. I installed the library with
gem install htmlentities
Do you have any idea why I'm having this problem?
Thank you.
EDITED:
I tried also with require 'rubygems' previously to any other require, but happens the same:
I tried require 'rubygems' but is happening the same:
irb(main):001:0> require 'rubygems'
=> false
irb(main):002:0> require 'htmlentities'
LoadError: no such file to load -- htmlentities
from (irb):2:in `require'
from (irb):2
from /usr/bin/irb:12:in `<main>'
Try to require 'rubygems' before the rest of your gems requirements.
rubygems is actually redefining the Kernel#require method to look for gems on your gempath. Whitout it ruby will just look for local/on path files.
It took me a lot but now I know how to fix it. It's about GEM_PATH.
# echo "export GEM_PATH=/var/lib/gems/1.9.2/" >> ~/.bashrc
# source ~/.bashrc
Now if I run irb:
# irb
irb(main):003:0> require 'htmlentities'
=> true
irb(main):004:0>
WOOT!
I used Bundler to generate a Gem skeleton for me. Within lib/foo.rb, I have the following:
require 'foo/client'
require 'foo/other'
Those two lines are supposed to require lib/foo/client.rb and lib/foo/other.rb, respectively. It builds without a problem, but when I go to test it with irb, I get a file not found error.
ruby-1.9.2-head :003 > require 'foo'
LoadError: no such file to load -- foo/client
from /home/ethan/.rvm/rubies/ruby-1.9.2-head/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /home/ethan/.rvm/rubies/ruby-1.9.2-head/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /home/ethan/.rvm/gems/ruby-1.9.2-head/gems/foo-0.1.0/lib/foo.rb:3:in `<top (required)>'
from /home/ethan/.rvm/rubies/ruby-1.9.2-head/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /home/ethan/.rvm/rubies/ruby-1.9.2-head/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from (irb):3
from /home/ethan/.rvm/rubies/ruby-1.9.2-head/bin/irb:16:in `<main>'
ruby-1.9.2-head :004 >
What is the correct way to require files within the same Gem? There must be something simple that I'm overseeing...
If your gem is called 'foo', then all you need to do is use bundle exec:
bundle exec your-script.rb
Without bundle exec, the load paths are not set up correctly.
Using irb, you use the bundle command bundle console.
chris#chris:~/oss/pp-adaptive$ irb
irb(main):001:0> AdaptivePayments
NameError: uninitialized constant Object::AdaptivePayments
from (irb):1
from /home/chris/.rbenv/versions/1.9.2-p290/bin/irb:12:in `<main>'
irb(main):002:0>
chris#chris:~/oss/pp-adaptive$ bundle console
irb(main):001:0> AdaptivePayments
=> AdaptivePayments
irb(main):002:0>
Note that once the gem is installed on your system, you may use it without bundler.
The current directory is not in the load path in Ruby 1.9. Try one of these:
require './client'
or
require_relative 'client'
If you are in IRB itself you may have to
require "rubygems"
require "foo"
if the library is a gem. Alternatively you can require the full path of the gem, but I wouldn't advise it since rubygems does require magic so reload! works in irb ( at least it does for rails console ).