3

我添加gem 'redis'到我的 Gemfile。冉bundle install。重新启动本地服务器,然后在我的控制台中运行 hello world 示例,结果出现错误。知道有什么问题吗?

[kudo (develop)]$ rails console
Loading development environment (Rails 3.2.3)
1.9.3p125 :001 > redis = Redis.new
=> #<Redis client v3.0.2 for redis://127.0.0.1:6379/0> 
1.9.3p125 :002 > redis.set("mykey", "hello world")
Redis::CannotConnectError: Error connecting to Redis on 127.0.0.1:6379 (ECONNREFUSED)
from /Users/pejman/.rvm/gems/ruby-1.9.3-p125@rails3tutorial2ndEd/gems/redis-3.0.2/lib/redis/client.rb:268:in `rescue in establish_connection'
from /Users/pejman/.rvm/gems/ruby-1.9.3-p125@rails3tutorial2ndEd/gems/redis-3.0.2/lib/redis/client.rb:263:in `establish_connection'
from /Users/pejman/.rvm/gems/ruby-1.9.3-p125@rails3tutorial2ndEd/gems/redis-3.0.2/lib/redis/client.rb:69:in `connect'
from /Users/pejman/.rvm/gems/ruby-1.9.3-p125@rails3tutorial2ndEd/gems/redis-3.0.2/lib/redis/client.rb:282:in `ensure_connected'
from /Users/pejman/.rvm/gems/ruby-1.9.3-p125@rails3tutorial2ndEd/gems/redis-3.0.2/lib/redis/client.rb:173:in `block in process'
from /Users/pejman/.rvm/gems/ruby-1.9.3-p125@rails3tutorial2ndEd/gems/redis-3.0.2/lib/redis/client.rb:248:in `logging'
from /Users/pejman/.rvm/gems/ruby-1.9.3-p125@rails3tutorial2ndEd/gems/redis-3.0.2/lib/redis/client.rb:172:in `process'
from /Users/pejman/.rvm/gems/ruby-1.9.3-p125@rails3tutorial2ndEd/gems/redis-3.0.2/lib/redis/client.rb:84:in `call'
from /Users/pejman/.rvm/gems/ruby-1.9.3-p125@rails3tutorial2ndEd/gems/redis-3.0.2/lib/redis.rb:608:in `block in set'
from /Users/pejman/.rvm/gems/ruby-1.9.3-p125@rails3tutorial2ndEd/gems/redis-3.0.2/lib/redis.rb:36:in `block in synchronize'
from /Users/pejman/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
from /Users/pejman/.rvm/gems/ruby-1.9.3-p125@rails3tutorial2ndEd/gems/redis-3.0.2/lib/redis.rb:36:in `synchronize'
from /Users/pejman/.rvm/gems/ruby-1.9.3-p125@rails3tutorial2ndEd/gems/redis-3.0.2/lib/redis.rb:607:in `set'
from (irb):2
from /Users/pejman/.rvm/gems/ruby-1.9.3-p125@rails3tutorial2ndEd/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
from /Users/pejman/.rvm/gems/ruby-1.9.3-p125@rails3tutorial2ndEd/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
from /Users/pejman/.rvm/gems/ruby-1.9.3-p125@rails3tutorial2ndEd/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'1.9.3p125 :003 > 
4

1 回答 1

7

最终变得非常简单。

要安装 redis(服务器),我运行(在终端中):

brew install redis

然后在一个单独的终端窗口中,我启动了服务器:

redis-server

现在我已经准备好了。我的 redis.set 命令现在在 rails 控制台中运行良好。

这是我发现的一个很好的资源,可以更深入地介绍它:http: //jimneath.org/2011/03/24/using-redis-with-ruby-on-rails.html

于 2012-11-29T22:30:20.317 回答