开发者

Rails - Custom config in environment config files?

开发者 https://www.devze.com 2023-01-07 04:37 出处:网络
Is it possible to define custom configuration in config/environments/*.rb or config/environment.rb file.

Is it possible to define custom configuration in config/environments/*.rb or config/environment.rb file.

Is y开发者_运维知识库es, how do i access them from my code (controller, model, library, helper)

Thanks.


I just use constants. Eg

AWS_PW = "ssss"

you can have different values in the different config files

Access them by their name. They're constants. They're available everywhere--controllers, views, models, etc

# eg
user.pw = AWS_PW

ADDED

Constants need to start with an upper case letter. Usual practice is to use all upper case, underscores, numerals, etc.

You need to restart Rails to pick up the new changes to the environment files since the appropriate environment file is read once.

Note: you can declare a hash as a constant. Eg

# in an environment file...
PARAMS = {}
PARAMS['default_pw'] = 'topsecret!'
0

精彩评论

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