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

2019-01-21 22:56发布

问题:

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 any view or controller. Does Rails 4 have an equivalent functionality for this type of thing?

The site-wide variable won't typically change often so it doesn't need protecting in any way.

For example, in my situation, I want to store the website's domain name. One for testing and one for live environments. Localhost for development and xxxxxx.com for production.

Any tips or pointers would help. I have Googled this extensively and solutions seem to be far too complicated to achieve what seems to be such a trivial task. What's the best elegant solution for Rails 4?

回答1:

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.


回答2:

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


回答3:

I can recommend good method to store variable. I use this on production
Passwords can be stored easier to .env file
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



回答4:

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"]


回答5:

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