Rails, populating a variable based on the environment - ruby-on-rails

I wanna populate a variable based on the environment it is used. For example the username must be test in the development environment while something else in the production environment. How can I achieve this?

Now I understand your problem, you should simply create a yml file.
See this Railscast.

Look at these options.
YAML Configuration. Also related video (Railscasts Video)
Configatron (Github configatron)
Also related answers

Related

What is the correct way to use config/credentials in Rails 6? And how do I stop my app from using /tmp/development_secret?

I'm working on a Rails application and I am attempting to organize my secret_key_base and related important secrets (Think API keys, database credentials, etc.)
I am trying to find a way to setup something like the following, under /config
/config
credentials.yml.enc
master.key
/config/credentials
development.yml.enc
development.key
production.yml.enc
production.key
test.yml.enc
test.key
First Question:
Is it that secret_key_base exists in /config/credentials.yml.enc, which is loaded (first?) and then the credentials are loaded for the environment rails is running in? Or should I create a different secret_key_base for each environment?
Second Question:
No matter what I do, when I run in development or test, tmp/development_secret loads first. In addition, whenever I try to access my secrets in development, (Rails.application.secret_key_base) as referenced here: What's the correct way of defining secret_key_base on Rails 6, I run into an issue where I only ever receive nil when looking for secrets I've defined in my development.yml.enc, which I assume is because it's not loading anything in that file, it's going to tmp/development_secret and not finding anything else (Maybe I'm wrong.)
Goals:
Stop tmp/development_secret from being created, and instead access
secrets using the specific .yml.enc file depending on the
environment.
Understand why /config/credentials.yml.enc exists if it doesn't
load in all the environments. If it doesn't, then it isn't clear when it loads.
Why?
Using config/database.yml as an example, I want to store different creds for each environment, but none of them in version control. (I want nobody but a few to have production.) However, I want to access them the exact same way in all of my environments. Not having creds load in production because of an issue with a .yml file will crash my app, so I don't want to load them differently in test/development.
Put together a blog post about this because searching for documentation on this feature is painful. It's a really easy thing because then only the master.key, and production.key would need loaded as ENV variables which is great. (Or possibility just one of them.)
This really should be a simple, out-of-the-box thing, but it's hard to find real documentation on best practices.
I've found the answer, at least the one I'm looking for. You can have your initializer load whatever file you want by overriding the defaults.
config.credentials.content_path = 'config/credentials/development.yml.enc'
config.credentials.key_path = 'config/credentials/development.key'
https://edgeapi.rubyonrails.org/classes/Rails/Application.html

Checking in configuration files with sensitive information into code repos

What is the best strategy in regards to checking in sensitive information into git? For example, database connection credentials, api keys, etc. For rails app, is it best to add environment files to .gitignore?
Thanks.
Your best bet is to use environment variables.
Check out these two links. The second link will show you how to keep your sensitive information secure by using environment variables to store sensitive data.
Rails Environment Variables
Environment Variables in Ruby on Rails
Nope, you don't need to ignore your env files - just remove all the secrets and use config variables ibstead... you can then refer to them using ENV['varname']
This gist shows one way of doing that using SECRET_KEY_BASE as an example (but you should do it with every sensitive key you have):
https://gist.github.com/cjolly/6265302

Best way to set environment-specific variables in rails

There are certain variables I would like to have in the development environment, and certain variables for the production environment. For example, in production mode, I might want to use a cache to speed up performance. I have couple of open-ended questions about this:
Question: What is the easiest and fastest way to set environment variables in a rails app? For example, ENV["USE_CACHE"] = true in production, and = false in development.
If you could point to a specific gem, and/or the specific files I'd need to touch, that would be most helpful. Thanks!
I learnt few things about ENV variables this discussion
You can set ENV varaibles by just appending
export USE_CACHE=true
to your .bashrc file.
...and in your application you can use
ENV["USE_CACHE"]
,
you can also check out figaro and dotenv which are application specific.
For your purpose just use
if Rails.env.eql?("development")
#do stuff
end
Its upto you choose easiest and fastest way!

Adding urls api keys in environment variable in ruby

I have a url that I am using in one of the controllers. Is there a better place to put this url? The url uses an API key and I was wondering if there is a better place to add this url and/or api key such that its not added in the controller class code and ergo more editable? If i add it as an environment variable or anything else how do i access it from my controller class? thank you. ITS A RUBY AND RAILS PROJECT
Using environment variables might be a good idea if you want to keep things like API keys and passwords out of your source code. Accessing them from within your code is done with the ENV object:
my_api_key = ENV['MY_API_KEY']
To use this technique, you need to set up the variables in your environment before launching your app, and how you do this depends on your local setup, and will likely also vary between development and production.
In development, you can simply set the environment vars in your shell, e.g. with bash:
$ export MY_API_KEY=foobar123abc
$ rails s
Now rails will start and have access to this environment variable. You can also set variables for just a single command:
$ MY_API_KEY=foobar123abc rails s
Depending on what the sevice/api is, you could set some of them to default development/test values in config/environments/development.rb (or test.rb):
ENV['MY_API_KEY'] = 'non_secret_api_key_that_can_be_shared_around'
Setting up environment variables in production will depend on how you're deploying your app. Phusion have an article on using environment variables in Passenger if your using that. There's also a useful article on using environment variables with Heroku which is worth a read even if you're not using them for deployment.
You can add it to application.rb file as
config.service {
:api_key => 'api_key'
}
Or better yet, add it to development.rb and production.rb files so that you can test it better.
You can access this api_key from controller like
Rails.application.config.service[:api_key]

Rails Environment

Is it possible to create user defined Rails environment.... By default support development,staging,production,test... Other than that is it possible for user defined environment.... If yes how? ... please suggest me on this ... thanks in advance...
You can define whatever environments you like by adding a my_awesome_environment.rb file to config/environments. After that, if you want to run a rake task in that environment you could do rake awesome:task RAILS_ENV=my_awesome_environment.
And, unless it's changed recently, there is no staging environment by default. Only development, test, production. As an example of "custom" environments, Cucumber, has its own cucumber environment when you set it up for a project.
If you have a more specific question, we might be able to give a more specific answer. As it stands, this is pretty open ended.

Resources