Where should I save site sitelide configuration settings for a Rails application?

I have a rails application and from the admin section of the site where I would like to enable / disable certain parameters, such as showing ads or embedding the Google Analytics tracking code.

Is there a best practice in Rails? One thought was to create a settings table and store the values ​​in it.

+5
source share
4 answers

If you are not going to tune the runtime, you can use something like the nifty-config rbates generator: http://github.com/ryanb/nifty-generators

. , , load_gateway_config.yml :

require 'ostruct'

raw_config = File.read(Rails.root + "config/gateway_config.yml")
GATEWAY_CONFIG = YAML.load(raw_config)[Rails.env].symbolize_keys

#allow dot notation access
GatewayConfig = OpenStruct.new(GATEWAY_CONFIG)

, , -

GatewayConfig.username

gem, , , - Ruby YAML.

+3

, . , .

.

def featured_specials
  @featured_specials = YAML::load_file("#{RAILS_ROOT}/config/featured_specials.yml")
end

def save_featured_specials
  config_file = "#{RAILS_ROOT}/config/featured_specials.yml"
  File.new(config_file, "w") unless File.exist?(config_file)
  File.open(config_file, "w") do |f|
    f.write(params['formvars'].to_yaml)
  end
  flash[:notice] = 'Featured Specials have been saved.'
  redirect_to :action => 'featured_specials'
end

: , .

+1

You can save the configuration in a directory config/. I know several applications that store their configs in this directory. (e.g. teambox.ymlin Teambox ).

0
source

I already looked at another answer, but for a quick and dirty way, you can reset the class variable in environment.rb after the initialization code block.

0
source

All Articles