开发者

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

When creating say a cms application where website specific details will vary depending on the website, where should I be storing开发者_如何学Python this information?

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


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜