Rails:我的rails应用程序根目录中的.env文件未加载

时间:2015-08-28 05:12:20

标签: ruby-on-rails ruby

我的应用根目录中有.env个文件。但是我在控制台中遇到这样的错误。如果我只是将信息输入,Carrierwave工作和雾工作,但我不想这样做。这仍处于开发阶段,尚未处于生产模式,而且我遇到了错误。

为什么.env文件没有被选中?

/config/initializers/carrierwave.rb:9: syntax error, unexpected tCONSTANT, expecting keyword_end (SyntaxError)
  config.fog_directory  = 'ENV['AWS_BUCKET']'

我原以为我必须将aws.rb文件放入config / initializers中,但这会产生错误。

.env

export AWS_BUCKET=jakesfolder
export AWS_ACCESS_KEY_ID=324fsdf454gd...
export AWS_SECRET_ACCESS_KEY=ghrth454g...

CarrierWave.rb

CarrierWave.configure do |config|
  config.fog_credentials = {
      :provider               => 'AWS',
      :aws_access_key_id      => ENV['AWS_ACCESS_KEY_ID'],
      :aws_secret_access_key  => ENV['AWS_SECRET_ACCESS_KEY'],
      :endpoint               => "https://s3.amazonaws.com",
      :region                 => ENV['AWS_REGION'] 
  }
  config.fog_directory  = 'ENV['AWS_BUCKET']'
end

Gemfile

source 'https://rubygems.org'

gem 'aws-sdk', '< 2.0'
gem 'fog'
gem 'rmagick', '~> 2.15.4'
gem 'carrierwave', '~> 0.10.0'
gem 'font-awesome-rails', '~> 4.4.0.0'
gem 'simple_form'
gem "cocoon"
gem 'devise', '~> 3.5.1'
gem 'bootstrap-sass', '~> 3.3.5'
gem 'rails', '4.2.0'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc

    group :development do
      gem 'byebug'
      gem 'web-console', '~> 2.0'
      gem 'spring'
      gem 'sqlite3'
    end

    group :production, :staging do
        gem 'pg', '~> 0.18.1'
        gem 'rails_12factor', '~> 0.0.3'
    end       

1 个答案:

答案 0 :(得分:1)

CarrierWave.rb文件中,更改:

config.fog_directory  = 'ENV['AWS_BUCKET']'

为:

config.fog_directory  = ENV['AWS_BUCKET']

您的额外'正在创建问题,并且您在该行上遇到语法错误。所以,只需删除它们就可以了。

更新

将此配置用于CarrierWave.rb文件:

CarrierWave.configure do |config|
  config.fog_credentials = {
      provider:                'AWS',
      aws_access_key_id:       ENV['AWS_ACCESS_KEY_ID'],
      aws_secret_access_key:   ENV['AWS_SECRET_ACCESS_KEY'],
      endpoint:                "https://s3.amazonaws.com",
      region:                  ENV['AWS_REGION']
  }
  config.fog_directory  = ENV['AWS_BUCKET']
end

然后,将所有export AWS_ACCESS_KEY_ID=324fsdf454gd...放入~/.bashrc文件中。