Where to store site specific information like site name, admin email, etc?

强颜欢笑 提交于 2019-12-23 11:34:19

问题


When creating say a cms application where website specific details will vary depending on the website, where should I be storing this information?

Things like: site name, support email, smtp settings, etc.?


回答1:


Assuming you mean configuration data for the application, here's what I do:

I create a config/app_config.yml file with my config information, like this:

site_name: This awesome site
support_email: me@mysite.com

Then, at the top of config/application.rb (right below the first 'require' statement), I add this:

# Load application-specific configuration from app_config.yml
require 'yaml'
APP_CONFIG = YAML.load(File.read(File.expand_path('../app_config.yml', __FILE__)))

Now, any time I need to access the configuration data, I can get to it via the APP_CONFIG hash, like so:

html = "For support, please e-mail #{APP_CONFIG['support_email']}"

Note, the above is for Rails 3. For rails 2, instead of loading the config in application.rb, you would put the statements into config/preinitializer.rb.

See http://asciicasts.com/episodes/226-upgrading-to-rails-3-part-2 for more details.




回答2:


Setting like this.

$ vi config/environments/development.rb
config.site_name = "Your Site Name"
config.site_url = "localhost:3000"

Also you can use 'application.rb' environments.

$ vi config/application.rb
module YourAppName
 class Application < Rails::Application
   config.site_name = "Your Site Name"

How to use it.

<%= YourAppName::Application.config.site_name %>
@url = YourAppName::Application.config.site_url

You can check your AppName from here

$ vi config/application.rb
module YourAppName
 class Application < Rails::Application


来源:https://stackoverflow.com/questions/5285052/where-to-store-site-specific-information-like-site-name-admin-email-etc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!