Rails 3:有没有办法将gem的配置参数放在environment.rb而不是foo.yml中?
我的 Rails 应用程序使用一个 gem,需要在 foo.yml 中指定一些配置参数:
development:
username: MyDevUserName
password: MyDevPassword
production:
username: MyPRODUserName
password: MyPRODPassword
我不想在源代码中使用密码,并且想要执行类似的操作:
development:
username: <%= ENV['THE_USERNAME'] %>
password: <%= ENV['THE_PASSWORD'] %>
production:
username: <%= ENV['THE_USERNAME'] %>
password: <%= ENV['THE_PASSWORD'] %>
但是,由于某种原因 <%= ENV['XXX' ] %>确实在我的 Settings.yml 文件中工作,但在我的 foo.yml 文件中不起作用(我猜测 foo gem 加载 .yml 文件,但它不允许解释)。
所以...
我想知道 Ruby/Rails 是否有一种通用方法来指定environment.rb而不是 foo.yml 文件中的变量?
例如,我可以有一个空的 foo.yml 文件并将以下内容添加到 environment.rb 中:
Foo::_something_._somethingelse =
{
:username => ENV['THE_USERNAME'],
:password => ENV['THE_PASSWORD']
}
My rails app uses a gem that requires some config params to be specified in foo.yml:
development:
username: MyDevUserName
password: MyDevPassword
production:
username: MyPRODUserName
password: MyPRODPassword
I dont want the password in my source code and want to do something like:
development:
username: <%= ENV['THE_USERNAME'] %>
password: <%= ENV['THE_PASSWORD'] %>
production:
username: <%= ENV['THE_USERNAME'] %>
password: <%= ENV['THE_PASSWORD'] %>
However, for some reason that <%= ENV['XXX'] %> does work in my Settings.yml file, but does not work in my foo.yml file (I'm guessing however the foo gem loads the .yml file it does not allow interpretation).
So...
I'm wondering if Ruby/Rails has a general-purpose way to specify the variables in environment.rb instead of a foo.yml file?
Can i for example have an empty foo.yml file and add the following to environment.rb:
Foo::_something_._somethingelse =
{
:username => ENV['THE_USERNAME'],
:password => ENV['THE_PASSWORD']
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:既然你在 Heroku...
Heroku 是一个不同的故事。您对 ENV 的使用可能与 Heroku 内置的某些用于处理配置变量(例如您正在使用的配置变量)的功能发生冲突。您需要(请鼓掌)...CONFIG VARS。有关如何在 Heroku 中设置配置变量的信息,请参阅Heroku 开发中心中的此页面部署,如何从您的应用程序访问它们,以及如何使其在本地工作。
编辑:下面是原始答案,仍然适用于一般(非heroku)情况
即使你把它放在environment.rb中,它仍然会在你的源代码中。
正确的方法是忽略版本控制中的 foo.yml 文件(例如,在 git 中,您可以将该文件添加到 .gitignore 文件中)。这样,您就可以在本地需要的地方拥有该文件,但它永远不会提交到您的存储库,因此您的信用不会暴露。在部署服务器上,您还必须手动创建该文件,因为当您从源代码部署时,部署将不会有它。
如果您使用 capistrano 进行部署,常见的方法是将文件放在 [app]/shared/config/foo.yml 中,然后将部署任务添加到从 [sharedpath] 到 [releasepath] 的软链接。 Capistrano 中的此类任务可能如下所示:
EDIT: Since you are on Heroku...
Heroku is a different story. Your use of ENV may be conflicting with some functionality built into Heroku for handling config vars such as the ones you are working with. You need (drumroll, please)... CONFIG VARS. See this page in the Heroku Dev Center for information on how to set config vars in your Heroku deployment, how to access them from your app, and how to make it all work locally as well.
EDIT: Below is the original answer, still applicable in the general (non-heroku) case
Even if you put it in environment.rb, it will still be in your source code.
The correct way to do this is to ignore the foo.yml file in your version control (for instance, in git you would add the file to the .gitignore file). That way, you have the file locally where you need it, but it never gets committed to your repository, so your creds aren't exposed. On your deployment server, you will have to manually create that file as well, since when you deploy from source code the deployment won't have it.
If you are using capistrano for deployment, a common approach would be to put the file in [app]/shared/config/foo.yml, then add a deployment task to softlink from [sharedpath] into [releasepath]. Such a task in capistrano might look like this: