开发者

Where should I store api key in rails3?

What is the best practic开发者_C百科e for storing/retrieving API keys in rails3?

Should I create my own application yaml and access it through there? If so, how?

Sorry for the noob question...


I use the settingslogic plugin for things like this. Very easy to use.

Add settingslogic to your Gemfile and bundle install:

gem 'settingslogic'

Create a directory for your settings and place the settingslogic yaml in there:

/my_app/config/settings/my_settings.yml

You can include default settings and per environment settings. The file looks like this:

defaults: &defaults
  api_key: abc123

development:
  <<: *defaults

test:
  <<: *defaults

production:
  <<: *defaults

Add this file: app/models/my_settings.rb, start up your app and you are good to go

class MySettings < Settingslogic
  source "#{Rails.root}/config/settings/my_settings.yml"
  namespace Rails.env
end

Now you can use call these settings from anywhere in the app like so:

MySettings.api_key
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜