Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 614
Michael Giovanni Pumo Avatar asked Dec 31 '13 13:12

Michael Giovanni Pumo


3 Answers

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.
like image 77
Simone Carletti Avatar answered Nov 06 '22 11:11

Simone Carletti


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
like image 9
ckruse Avatar answered Nov 06 '22 09:11

ckruse


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

like image 4
Philidor Avatar answered Nov 06 '22 10:11

Philidor