Search code examples
ruby-on-railsherokuredistogo

Setting up Redis To Go with Heroku and Rails 4.0.0


I am attempting to setup Redis To Go on my Rails 4 app. I want to be able to deploy it to Heroku as well.

So far, this is what I've done:

Through the dashboard.heroku site, I used the one click install for the Nano version of Redis To Go to install the addon to my app.

I added gem 'redis' to my gemfile.

In config/environments/development.rb I added this line:

ENV["REDISTOGO_URL"] = 'redis://redistogo:b9fc604b1c86a1f6c232ce1dd16cd989@albacore.redistogo.com:10280/'

Then, I created a config/initializers/redis.rb file which looks like this:

uri = URI.parse(ENV["redis://redistogo:b9fc604b1c86a1f6d872ce1dd16cd989@albacore.redistogo.com:10280/"] || "redis://localhost:6379/")
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)

When running a Redis command in my console now, I get this error:

Redis::CannotConnectError: Error connecting to Redis on 127.0.0.1:6379 (ECONNREFUSED)

What am I doing wrong here, and what do I need to do to ensure that I can test in development and deploy to Heroku without any issues?


Solution

  • ENV["REDISTOGO_URL"] should be in the environment on Heroku. I'd remove it from config/environments/development.rb altogether and change the redis.rb initializer to:

    uri = URI.parse(ENV.fetch("REDISTOGO_URL", "redis://localhost:6379/"))
    REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
    

    As long as that ENV var isn't set in development, it'll fall back to the local redis installation.