Hi is it possible to read from an controller the settings yml? Im using rails 4.2.0
I want to store some global information in the settings.yml to display those in the view
at the moment I try this way:
settingsfile = File.read(Rails.root + "./config/settings.yml")
settingsfile = YAML.load(settingsfile)
settingsfile["default"]
works but when I want to go deeper
settingsfile["default"]["info"]
its empty
Yml File
default:
supported_languages:
de: Deutsch
fr: Francais
en: English
it: Italiano
sv: Svenska
pt: Português
nl: Nederlands
es: Español
production:
info:
version: 2.0.0
datum: 11.05.2015
without using gem..you can use something like ..
your dev/config/email.yml
development:
:address: smtp.service.com
:port: 25
:user_name: mike#gmail.com
:password: mikepassword
production:
:address: smtp.gmail.com
:port: 587
:authentication: plain
:user_name: mike#gmail.com
:password: mikepassword
:enable_starttls_auto: true
you can load this yml file simply like....
email_settings = YAML::load(File.open("#{Rails.root.to_s}/config/email.yml"))
ActionMailer::Base.smtp_settings = email_settings[Rails.env] unless email_settings[Rails.env].nil?
also you may use - Rails::Application.config_for(:email) as shown in the documentation for Rails 4+ onwards.
Take a look at the gem rails_config.
You could try a solution such as BetterSettings or settingslogic which provide a simple way to load YML files into a settings object.
Both libraries allow to easily access settings using method calls:
class Settings < BetterSettings
source Rails.root.join('config/settings.yml'), namespace: :default
end
which would allow you to access settings like:
Settings.info.version # '2.0.0'
Settings.supported_languages.es # 'Español'
In this example, we are using :default as the namespace, but you could also use Rails.env to scope the settings to a specific environment.
You might try:
#settingsvariable = Settings.default.supported_languages.de
This approach worked in my app.
Related
Let's assume I have a config/secrets.yml file that contains:
development:
app_name: MyApp
secret: <%= ENV['SECRET_VALUE'] %>
If I set SECRET_VALUE with a newline, it will break. E.g:
export SECRET_VALUE=$(printf "hello\nworld")
Then when I start my rail application I get this error:
/usr/local/lib/ruby/3.0.0/psych.rb:457:in 'parse': (<unknown>): could not find expected ':' while scanning a simple key at line 4 column 1 (Psych::SyntaxError)
While debugging the issue, I realized that the literal value of ENV['SECRET_VALUE'] is added to the yaml file before parsing it. That means if I wanted to achieve what I'm trying to do, I would have to do something like:
export SECRET_VALUE=$(printf "|\n hello\n world")
This works but this is very ugly and I can't be the only one who think this behaviour is absurd?? Is there a better way to do this?
EDIT:
I tried adding quotes around the value:
development:
app_name: MyApp
secret: "<%= ENV['SECRET_VALUE'] %>"
It "works" but the newline gets removed from the string...
root#4e4431bae32e:/app# rails console
Loading development environment (Rails 7.0.3)
irb(main):001:0> puts ENV['SECRET_VALUE']
hello
world
=> nil
irb(main):002:0> puts Rails.application.secrets[:secret]
hello world
=> nil
It's important that the newline remains for my use case.
You can use an escaped quoted string. YAML's string format is similar enough to a superset of Ruby's for String#dump to work:
development:
app_name: MyApp
secret: <%= ENV['SECRET_VALUE'].dump %>
I am working on a MVC project in Ruby on Rails. I have the actual code and i have to understand it in next few days and then try to upgrade it.
I have a home-page with a textbox which ask you the e-mail for resistration. The problem is that when i push the submit button it gives me
Recaptcha::Recaptcha Error - No site key specified
I know that there are many posts about this problem but i don't manage to resolve it.
Here is my .env file
RECAPTCHA_SITE_KEY=6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy
RECAPTCHA_SECRET_KEY=6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx
OMNIAUTH_PROVIDER_KEY=false
OMNIAUTH_PROVIDER_SECRET=false
ADMIN_NAME=false
SECRET_KEY_BASE=false
GA_PROVIDER_KEY=false
TWILIO_SID=false
TWILIO_TOKEN=false
TWILIO_NUMBER=false
Please help me with this.
You need to configure recaptcha to pull the keys from your secrets.yml by adding an initializer file, config/initializers/recaptcha.rb:
# config/initializers/recaptcha.rb
Recaptcha.configure do |config|
config.site_key = Rails.application.secrets[:RECAPTCHA_SITE_KEY]
config.secret_key = Rails.application.secrets[:RECAPTCHA_PRIVATE_KEY]
# Uncomment the following line if you are using a proxy server:
# config.proxy = 'http://myproxy.com.au:8080'
end
End in secrets.yml RECAPTCHA_SITE_KEY and RECAPTCHA_PRIVATE_KEY make sure that it is written in capital letters
im using a gem for a sms service, and i have to put the params in the specific environment config file.
All is ok with static params, but if i call this params from a db, starting the webrick i have an error:
(ActiveRecord::ConnectionNotEstablished)
this is my original code in development.rb
Skuby.setup do |config|
config.method = 'send_sms_classic' #default
config.username = 'myusername'
config.password = 'mypassword'
config.password = Setting.where(campo: 'skebby_password').valore
config.sender_string = 'company' #optional
config.sender_number = '39123456790' #optional
config.charset = 'UTF-8' #skebby default is ISO-8859-1
end
changing something, like:
config.password = Setting.where(campo: 'skebby_password').valore
it goes down!
How can i fix?
Do you think it's ok to do this in your config/environment. I would think that such would be better suited for your config/initializers, within your initializer, your db connection is already setup.
BTW, if you're using different parameters for your environments, I think you should rather use a configuration file, which would set your params per environment and you could then use that.
Anybody know how to set ENV variables in production.rb using the new secrets.yml?
I'm getting key: wrong number of arguments (0 for 1) for the below:
production.rb:
# DOES NOT WORK
ENV["SOME_STUFF_KEY"] = Rails.application.secrets.some_stuff.key
ENV["SOME_STUFF_SECRET_KEY"] = Rails.application.secrets.some_stuff.secret_key
# NOTE THAT THIS WORKS:
# ENV["SOME_STUFF_KEY"] = "abcdefg";
# ENV["SOME_STUFF_SECRET_KEY"] = "123456789123456789";
secrets.yml:
production:
some_stuff:
key: abcdefg
secret_key: 123456789123456789
I haven't found any information about nesting like you did with some_stuff. The release notes and all other blog posts just use flat keys as an example:
development:
secret_key_base: 3b7cd727ee24e8444053437c36cc66c3
some_api_key: SOMEKEY
Rails.application.secrets.some_api_key returns SOMEKEY in the development environment.
That's why someone created the dot_secrets gem ("Enable dot syntax on nested Rails.application.secrets")
To get nested values like yours you need to use this syntax.
Rails.application.secrets.some_stuff[:secret_key]
I'm trying define a connection to an external storage provider. To open the connection, normally I would do
storage = Fog::Storage.new {lots more stuff here}
This means putting very secret credentials in the code every where I want to access the storage object. There has to be a better way?
If you define a constant in a file in config/initializers, that file will be required at app start and the constant will be generally available. For instance:
config/initializers/config.rb
Storage = Fog::Storage.new { stuff }
app/models/mymodel.rb
do_something_with(Storage)
There is. Create a fog.yml or something, and place it in your config folder (you'll want to ignore this in version control).
Then set the credentials:
credentials: &credentials
username: yourname
password: yourpass
development:
<<: *credentials
test:
<<: *credentials
production:
<<: *credentials
(I don't use fog so I don't really know what information you need). Then make a new initializer file, fog.rb or something, and place it in config/initializers. Put this inside so it gets loaded when the app is initialized:
config = YAML.load_file("#{Rails.root}/config/fog.yml")[Rails.env]
FogStorage = Fog::Storage.new { config['username'], config['password'] }
Then you can use that global FogStorage anywhere.
Again, be sure you ignore the credentials file for version control, and just make a symlink to it in deployment.