问题
I've got the following CarrierWave initializer which works fine on my Heroku/MONGOHQ/GridFS env :
CarrierWave.configure do |config|
config.storage = :grid_fs
uri = URI.parse(ENV['MONGOHQ_URL'])
config.grid_fs_database = File.basename(uri.path)
config.grid_fs_host = uri.host unless uri.host.blank?
config.grid_fs_port = uri.port unless uri.port.blank?
config.grid_fs_username = uri.user unless uri.user.blank?
config.grid_fs_password = uri.password unless uri.password.blank?
config.grid_fs_access_url = '/gridfs'
config.cache_dir = "uploads"
config.root = Rails.root.join('tmp')
end
but, when I try to run the code locally (in developement) I get the following error:
`split': bad URI(is not URI?): (URI::InvalidURIError)
here is the full stack : http://pastie.org/1630069 I tried to add require 'uri/generic' on top of initializer but doesn't works.
Does anybody know way ? Thanks in advance luca
回答1:
another solution would be to add a ".env" file in the project root and define there environment variables like:
MONGOHQ_URL=mongodb://someuser:somepass@paulo.mongohq.com:10040/appid
回答2:
As suggested by KenB the ENV['MONGOHQ_URL'] was not setted on my local machine developement environment :
lsoave@ubuntu:~/rails/heroku/mp3upload$ rails c
Loading development environment (Rails 3.0.5)
ruby-1.9.2-p136 :001 > ENV['MONGOHQ_URL']
=> nil
ruby-1.9.2-p136 :002 >
this was a branch without the initializer, so on my local machine I had to skip that. I did it like that:
if ENV['MONGOHQ_URL']
CarrierWave.configure do |config|
config.storage = :grid_fs
uri = URI.parse(ENV['MONGOHQ_URL'])
config.grid_fs_database = File.basename(uri.path)
config.grid_fs_host = uri.host unless uri.host.blank?
config.grid_fs_port = uri.port unless uri.port.blank?
config.grid_fs_username = uri.user unless uri.user.blank?
config.grid_fs_password = uri.password unless uri.password.blank?
config.grid_fs_access_url = '/gridfs'
config.cache_dir = "uploads"
config.root = Rails.root.join('tmp')
end
end
I think that should be a very better way to skip a Ralis 3.0.5 initializer during the boot process, conditionally to the ENV['MONGOHQ_URL'] parameter value.
If you have a better way, could you please share it ? Many thanks :-) luca
回答3:
In your initializer, you can do this: URI.parse(ENV['MONGOHQ_URL'] || 'some_other_link')
as specified in the MongoHQ Heroku docs
来源:https://stackoverflow.com/questions/5186469/cannot-split-bad-uriis-not-uri