问题描述
我正在使用具有用户身份验证并提供管理员帐户的Rails应用程序.在管理员帐户中,我创建了一个用于站点范围设置的页面.
I am working on a Rails application that has user authentication which provides an administrators account. Within the administrators account I have made a page for sitewide settings.
我想知道创建这些设置的规范是什么.举例来说,我希望其中一项设置是更改应用程序名称的名称,或更改标题的颜色.
I was wondering what the norm is for creating these settings. Say for example I would like one of the settings to be to change the name of the application name, or change a colour of the header.
我正在寻找的是让某人解释基本过程/方法-不一定是特定的代码-尽管那会很棒!
What I am looking for is for someone to explain the basic process/method - not necessarily specific code - although that would be great!
推荐答案
对于不需要存储在数据库表中的常规应用程序配置,我想在 config 目录.对于您的示例,它可能看起来像这样:
For general application configuration that doesn't need to be stored in a database table, I like to create a config.yml
file within the config directory. For your example, it might look like this:
defaults: &defaults
app_title: My Awesome App
header_colour: #fff
development:
<<: *defaults
test:
<<: *defaults
app_title: My Awesome App (TEST ENV)
production:
<<: *defaults
此配置文件从 config/initializers 中的自定义初始化程序加载:
This configuration file gets loaded from a custom initializer in config/initializers:
Rails 2.x:
Rails 2.x:
APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/config.yml")[RAILS_ENV]
Rails 3.x:
Rails 3.x:
APP_CONFIG = YAML.load_file("#{Rails.root}/config/config.yml")[Rails.env]
然后您可以使用以下方法检索值:
You can then retrieve the value using:
title = APP_CONFIG['app_title']
有关详细信息,请参见此Railscast .
See this Railscast for full details.
这篇关于Rails应用程序设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!