开发者

Why does adding redis to Gemfile fix error with: require 'redis'

I have had a problem trying use the redis (2.1.1) gem with rails. I've fixed the problem but I don't know why the fix works and would like to understand better.

I am using Snow Leopard. After much troubleshooting, I removed the versions of Ruby, Rails and RubyGems and then used MacPorts to install Ruby 1.8.7 patch level 334 and RubyGems 1.6.0. Rails is now a fresh install of version 3.0.5. Using "which -a" reveals that there is only one version of ruby and gem in the path.

I created a new rails application and added a simple controller, with the following code:

require 'rubygems'
require 'redis'

class HomeController < ApplicationController
  def index
    redis = Redis.new
    redis.set "mykey", "myvalue"
  end
end

When I start "rails server" and load page http://localhost:3000/home/index, I get the following error message:

LoadError in HomeController#index

no such file to load -- redis
Rails.root: /Users/Scott/Projects/Rails/myapp

Application Trace | Framework Trace | Full Trace
app/controllers/home_controller.rb:2
This error occurred while loading the following files:
   redis

This error does not occur when using the redis gem via irb.

~/Projects/Rails/myapp Scott $ irb
irb(main):001:0> require 'rubygems'
=> tru开发者_JS百科e
irb(main):002:0> require 'redis'
=> true
irb(main):003:0> r = Redis.new
=> #<Redis client v2.1.1 connected to redis://127.0.0.1:6379/0 (Redis v2.2.1)>

The only way that I have found to fix this problem with my rails app is to add redis to Gemfile. I don't perform any other actions after changing the default Gemfile file created by "rails new myapp", I just reload the page and it works.

source 'http://rubygems.org'

gem 'rails', '3.0.5'
gem 'sqlite3'
gem 'redis'

I've read through many similar questions here and elsewhere about why the require statement might fail, but haven't found anything that I've understood to explain my situation. In case it is helpful, the gem paths returned by "gem env" are:

RubyGems Environment:
  - RUBYGEMS VERSION: 1.6.0
  - RUBY VERSION: 1.8.7 (2011-02-18 patchlevel 334) [i686-darwin10]
  - INSTALLATION DIRECTORY: /opt/local/lib/ruby/gems/1.8
  - RUBY EXECUTABLE: /opt/local/bin/ruby
  - EXECUTABLE DIRECTORY: /opt/local/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86-darwin-10
  - GEM PATHS:
     - /opt/local/lib/ruby/gems/1.8
     - /Users/Scott/.gem/ruby/1.8
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - http://rubygems.org/

Ruby appears to be using the same path:

~/Projects/Rails/armatree Scott $ irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> puts Gem.path
/Users/Scott/.gem/ruby/1.8
/opt/local/lib/ruby/gems/1.8
=> nil

I notice that the two paths are in different orders, however the path in /Users does not contain any gems.

Any insight that can be provided is appreciated. Thanks for reading this far.


Bundler disables system gems and only makes gems which are listed in the Gemfile (and their dependencies) available to the app. That's why any gems which your app requires should be in the Gemfile.

It's also worth noting that bundler 'requires' gems for you unless you tell it not to. So, once you've told it to use the redis gem in the Gemfile, you needn't require it elsewhere in the app.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜