Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

ruby on rails - Redis looking for env redis url variable not sure where to put env variable bad URI(is not URI?): (URI::InvalidURIError)

I am quite new to redis. This rails application has a redis.rb file in config/initializers

uri = URI.parse(ENV["REDIS_URL"])
$redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)

The redis url is on heroku config.

I can't just replace REDIS_URL with the REDIS_URL from heroku config.

I am getting a URI parse error

 bad URI(is not URI?):  (URI::InvalidURIError)

my question is where should I place the redis url ? where is it searching the env variable from?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm guessing you're getting this when doing rake. The problem is that when rake-ing, your environment variables aren't set, which leads to this error (info at https://devcenter.heroku.com/articles/rails-asset-pipeline). To overcome, use a conditional intializer instead, e.g.:

if ENV["REDISCLOUD_URL"]
    uri = URI.parse(ENV["REDISCLOUD_URL"])
    $redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
end

P.S. alternatively, use this but note that according to Heroku:

Using this labs feature is considered counter to Heroku best practices. This labs feature can make your builds less deterministic and require re-deploys after making config changes. Ideally your app should be able to build without config.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...