0

C 레일에 표시되지, 환경 변수가 나는 application.ymlEnvirontment 변수 내가 레일에 루비에 대해 물어 싶어

defaults: &defaults 
    STORE_URL: https://localhost:3000/ 

development: 
    <<: *defaults 

test: 
    <<: *defaults 

production: 
    <<: *defaults 

이 같은 코드를 가지고 application.yml

를 사용하여 설정할 수 있습니다 또한 application.rb

Bundler.require(*Rails.groups) 

if File.exists?(File.expand_path('../application.yml', __FILE__)) 
    config = YAML.load(File.read(File.expand_path('../application.yml', __FILE__))) 
    config.merge! config.fetch(Rails.env, {}) 
    config.each do |key, value| 
    ENV[key] ||= value.to_s unless value.kind_of? Hash 
    end 
end 

의 구성을 설정하고 .gitignore이 코드를 추가

,
config/appication.yml 
.project 

나 단말기에서 테스트 할 때 출력이 같아야,

[1] pry(main)> ENV 
=> {"Test1"=>"Tester1", 
    "Test2"=>"Tester2", 
    "Test3"=>"Tester3"} 

는 단지 추가해야 몇몇 키와 값 때 레일 3.0.20 루비 1.9 run rails c development

. 3p545 형식으로 테스트하거나 application.yml .but, Rails 4.1.5 및 Ruby 2.0.0p541에 새로운 키 및 값을 추가하면 간단 해집니다.

전체 Application.rb

require File.expand_path('../boot', __FILE__) 

require 'rails/all' 

# Require the gems listed in Gemfile, including any gems 
# you've limited to :test, :development, or :production. 
Bundler.require(*Rails.groups) 

if File.exists?(File.expand_path('../application.yml', __FILE__)) 
    config = YAML.load(File.read(File.expand_path('../application.yml', __FILE__))) 
    config.merge! config.fetch(Rails.env, {}) 
    config.each do |key, value| 
    ENV[key] ||= value.to_s unless value.kind_of? Hash 
    end 
end 

module ModuleUpgrade 
    class Application < Rails::Application 
    end 
end 

도움이 필요하십니까? 감사합니다.

답변

0

두 가지를 변경했습니다. 파일을로드하고 취득 해, 환경에 대한 서브 해시를 병합하는 방법을 방법 :

Bundler.require(*Rails.groups) 

file = Rails.root.join('config', 'application.yml') 
if File.exists?(file) 
    config = YAML.load(file).fetch(Rails.env, {}) 
    config.each do |key, value| 
    ENV[key] ||= value unless value.kind_of?(Hash) 
    end 
end 
+0

을 application.yml에 설정하고 레일즈 C 및 유형 ENV에서 테스트 할 때 표시됩니다. @spickermann – agstwn21

+0

미안하지만 당신이하고 싶은 말을 이해하지 못합니다. – spickermann

+0

네 .. 나도 레일에 루비가있어. 그래서 나는 단지 무슨 뜻인지 짐작한다. @spickermann – agstwn21

0

나는 생각하지 않는다 당신이 application.yml, File.expand_path('../application.yml', __FILE__)에 지정한 경로를 정확합니다. 이 시도 : 당신이 스스로를 롤백 할 수없는 경우

app_config = File.join(Rails.root, 'config', 'application.yml') 
if File.exists?(app_config) 
    config = YAML.load(File.read(app_config)) 
    config.merge! config.fetch(Rails.env, {}) 
    config.each do |key, value| 
    ENV[key] ||= value.to_s unless value.kind_of? Hash 
    end 
end 

대안 figaro gem 시도가 당신을 위해 쉽게 만들 수 있습니다.

+0

이미 시도했지만 오류가 발생했습니다. @ glenn-gillen – agstwn21

+0

'Rails.root'는 nil이라는 것을 의미합니다. 이는 Rails 자체가 그렇지 않다는 것을 의미합니다 아직로드되지 않았습니다. 전체'application.rb' 파일을 여기에 붙여 넣을 수 있습니까? –

+0

이미 @ glenn-gillen 위의 내 스레드를 확인할 수 있습니다. – agstwn21