Rubyroid Labs Blog
Tech Tips and Freebies

Complete Guide on Creating Environments in Rails

Complete Guide on Creating Environments in Rails

Reading Time: 2 minutes

Developing a successful Ruby on Rails project is closely connected with all sorts of environments: development, testing, staging and production. But how can I create them? Check our guide for Ruby on Rails environments.

Why do I need a few environments?

If you are new to coding routine, the main question you might have is “Why at all do I need a few environments?”

The answer is pretty easy: it is connected with different stages of software development:

  1. Actual Development of the Product
  2. Testing the Product
  3. Showing it in Live to Users

These are the main stages any software development comes through. Missing any of those will make the product impossible, useless or will put it at a great risk.

Creators of Ruby on Rails shared this idea, so they decided to provide you with 3 environments for that by default. This way from the very beginning you will have the following 3 environments: development, testing and production. Conducting activities in these 3 environments separately is important, since it lets you troubleshoot the bugs easier and not blush in front of your customers for some reckless commits.
“OK, so I have 3 environments by default. But what did you say something about staging environment?” Yes, we did. Staging environment is required for cases when you have already gone live and don’t want to take risks of delivering everything to live right away. It lets you do all SMOKE and MAT tests with no rush and simply be sure that the new features you deliver wouldn’t break the old code.
Unfortunately, you would have to roll up your sleeves to get yourself a staging environment (or as many additional environments, as you need). Luckily, we have prepared an easy instruction for you.

How to create a new environment

To create a new environment you will need the following things:

  1.  file
  2. database configuration entry in   (of course, if you use database)
  3. secret key base entry in   (if you work wth Rails 4.1+)

Let’s start with new environment file:

By the way, there’s a shorter way to do this – simply copy production.rb.
Now let’s add your database to config/database.yml.

For a new secret key base in config/secrets.yml use Rake command:

Make sure you have different initializers configured for different environments. You might use rack-mini-profiler for that:

Now you have a new shiny environment at your disposal. Go prepend your commands with a new RAILS_ENV values:

As you see, this wasn’t that difficult. Our developers do similar thing for almost any project we have. And do you use staging environments? Share with us in the comments!

Vera Rabkina

Business Analyst at Rubyroid Labs


  •  
  •  
  •  
  •  
  •  
  • 1

Leave a Reply

Your email address will not be published. Required fields are marked *