Where can I store site-wide variables in Rails 4?

前端 未结 5 1118
眼角桃花
眼角桃花 2020-12-13 14:41

I am new to Rails and come from a ColdFusion background, where we would store global / site-wide variables in the \'application\' scope. This persists the variable across an

相关标签:
5条回答
  • 2020-12-13 15:06

    The simplest, basic and default way is to use the Rails.application.config store.

    Rails.application.config.my_config = 'foo'
    

    You can assign a config in your environment:

    # application.rb
    module MyApp
      class Application < Rails::Application
        config.my_config = 'foo'
      end
    end
    

    and read it with

    Rails.application.config.my_config
    # => 'foo'
    

    This approach works well for very simple applications, but if you want something more advanced there are several gems available.

    I'm currently using SimpleConfig. The main advantages are:

    • per-environment configuration. You can configure default configurations for the application, then override defaults with environment specific configurations
    • local.rb file for custom overrides
    • capistrano-like configuration style
    • it works nicely with the dotenv gem, very useful to avoid storing sensitive credentials in your repo.
    0 讨论(0)
  • 2020-12-13 15:08

    This sounds like a perfect example for configuration values stored in config/environments/production.rb and config/environments/development.rb. Just store any value there:

    config.my_special_value = 'val'
    

    And access it in your application like this:

    Rails.application.config.my_special_value
    

    Always the value of your environment is active.

    If you just want to have a „global“ value, store it in your application controller. All your view controllers are derived from your app controller, so you can save any value there as an instance or class variable:

    class ApplicationController < ActionController::Base
      MY_CONSTANT_VALUE = "foo"
    end
    
    class MyViewController < ApplicationController
      def index
        raise MY_CONSTANT_VALUE.inspect
      end
    end
    

    You also could implement an helper:

    # app/helpers/application_helper.rb
    module ApplicationHelper
      FOO = "bar"
    end
    
    # app/controllers/foo_controller.rb
    class FooController < ApplicationController
      def index
        raise FOO
      end
    end
    
    0 讨论(0)
  • 2020-12-13 15:08

    I can recommend good method to store variable. I use this on production
    Passwords can be stored easier to .env file http://i.stack.imgur.com/jbcAO.png
    like this

    #Root dir create file ".env"
    PASSWORD=123456
    

    and load password

    #Somewhere in app
    ENV['PASSWORD'] #=> 123456
    

    it works I hope will help you

    enter image description here

    0 讨论(0)
  • 2020-12-13 15:08

    In rails there is gem named as

    gem 'dotenv-rails'
    

    By using it we can assign the variables to system level and used in application.

    By using simple steps First create a simple filed in system level at any place with named extension .env

    //in application.rb        
    require 'dotenv'
    Dotenv.load('path-of-your-file.env')
    

    And restart your application

    Source Please got the link for the desscription of dot env gem

    0 讨论(0)
  • 2020-12-13 15:16

    You can use gem figaro

    write your variables in config/application.yml

    HELLO: world
    development:
      HELLO: developers
    production:
      HELLO: users
    

    Then you can fetch

    ENV["HELLO"]
    
    0 讨论(0)
提交回复
热议问题