2016-12-15 3 views
0
Arbeits

ich eine Datei mit dem Namen haben frontend_configuration.rb, die wie folgt aussieht:Rails 5 benutzerdefinierte initializer nicht

class FrontEndConfiguration 
    class << self 
    attr_accessor :base_url 
    end 
end 

In meinem development Config habe ich eine Zeile:

FrontEndConfiguration.base_url = "http://localhost:4200" 

Wenn ich versuche, meine zu laufen Rails Server, es gibt mir einen Fehler, der sagt:

uninitialized constant FrontEndConfiguration (NameError) 

Ich folge diesem Stackoverflo w antwort hier: Rails 3/Setting Custom Environment Variables

Irgendwelche Ideen, warum Schienen meinen kundenspezifischen Initialisierer nicht feststellt?

Ich verwende nur Rails 5 API-Modus.

Antwort

2

Versuchen Sie dies.

# config/frontend.yml: 
production: 
    environment: production 
    base_url: http://your_url 
    . 
    . 
    . 
development: 
    environment: sandbox 
    base_url: http://your_url 
    . 
    . 
    . 


# config/application.rb 
module MyApp 
    class Application < Rails::Application 
    config.frontend = config_for(:frontend) 
    end 
end 

Und jetzt

Rails.configuration.frontend['base_url'] 
# => production_base_url or development_base_url 
+0

Ist dies der empfohlene Weg, um es in Rails zu tun 5 jetzt? Es funktioniert sicherlich für das, was ich erreichen möchte. – Zhang

+0

@ Zhang ja, es ist. Sehen Sie die [Dokumentation] (http://guides.rubyonrails.org/configuring.html#custom-configuration) –

Verwandte Themen