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. config/environments/YOUR_ENVIRONMENT.rb file
  2. database configuration entry in config/database.yml (of course, if you use database)
  3. secret key base entry in config/secrets.yml (if you work wth Rails 4.1+)

Let’s start with new environment file:

[ruby]

$ cat config/environments/staging.rb
# Just use the production settings
require File.expand_path(‘../production.rb’, __FILE__)

Rails.application.configure do
# Here override any defaults
config.serve_static_files = true
end
[/ruby]

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.

[ruby]
# Production settings for local development and profiling
staging:
database: db_profile
..
[/ruby]

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

[ruby]$ rake secret
c975f1417b60097ecfc17e308f0d8fc502f1e2534b14ef41527d703923db9e875ad4eeb779a74c732bb6c5747c3b56d84fe7f38554089522a2f557c587766fcc
[/ruby]

[ruby]..
test:
secret_key_base: 40bf0f5019e785b6b44a29f1680febbcb06db8dd64f835986c6686bebddf304b67f8a9a6dffcc862f2586edc60921d0b736e3e0b1833eea2431767d2a0d1f9cc

# Add this new entry with the generated key base
staging:
secret_key_base: c975f1417b60097ecfc17e308f0d8fc502f1e2534b14ef41527d703923db9e875ad4eeb779a74c732bb6c5747c3b56d84fe7f38554089522a2f557c587766fcc
..
[/ruby]

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

[ruby]$ cat config/initializers/rack_profiler.rb
if Rails.env.development? || Rails.env.staging?
require ‘rack-mini-profiler’

# Initialization is skipped so trigger it
Rack::MiniProfilerRails.initialize!(Rails.application)

# Needed for staging env
Rack::MiniProfiler.config.pre_authorize_cb = lambda { |env| true }
Rack::MiniProfiler.config.authorization_mode = :allowall
end
[/ruby]

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

[ruby]$ RAILS_ENV=staging rake db:create [/ruby]

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!

How useful was this post?

Click on a star to rate it!

Average rating 4.8 / 5. Vote count: 10

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?


Author

Daria Stolyar is a Marketing Manager at Rubyroid Labs. You can follow her at Linkedin.

Write A Comment