How I Create a New Ruby on Rails Project

Nov 11, 2014 - 5 min read
How I Create a New Ruby on Rails Project
Share

In this article you'll see how I like to create a new Ruby on Rails project with all the dependencies isolated nicely.

You can either watch the video or follow the instructions in the article.

Install Ruby

I've grown accustomed to RVM, and so I'm going to show you how to install Ruby using RVM.

The reason I'm using RVM is because it makes it easy to switch from one project to the next, and have all the dependencies in a separate folder that is specific to that project.

To get that, I always use a separate gemset for each project.

If you are on a mac, you can just copy paste these two lines into your terminal (see the RVM install page and the post install configuration section for more info).

$ gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
$ \curl -sSL https://get.rvm.io | bash -s stable

Once that's done, installing a version of Ruby is so easy.

$ rvm install 2.5.1
$ rvm use 2.1.1
$ ruby -v

You should see something like ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17].

And if you want to make that version of Ruby the default, you can use the --default flag.

$ rvm --default use 2.5.1

You can check out the list ruby versions you have installed with rvm list.

Create an RVM gemset

To keep your gems together, you can create gemsets. Like I said previously, I like to use on gemset per project. Here's how you do that.

$ rvm gemset create my_cool_app
ruby-2.5.1 - #gemset created /Users/cezar/.rvm/gems/ruby-2.5.1@my_cool_app
ruby-2.5.1 - #generating my_cool_app wrappers........

And to use that gemset you use this.

$ rvm 2.5.1@my_cool_app
Using /Users/cezar/.rvm/gems/ruby-2.5.1 with gemset my_cool_app

You can confirm that with rvm gemset list. It will mark the current gemset with a => sign.

Install Ruby on Rails

Just because I like to keep all the dependencies separate for each project, I like to start with a fresh gemset.

$ cd ~/Work
$ rvm use 2.5.1@my_cool_app --create
$ gem install rails

As you can see, that's another way of creating the gemset if it doesn't exist.

How to create a new Ruby on Rails project

Now that you have the latest version of Ruby, and the latest version of Rails, you can go ahead and create your project.

$ rails new my_cool_app --skip-test --skip-bundle --database=postgresql

I like to use RSpec instead of Minitest, and I also like to skip the initial bundle install because there are a few more tweaks I need to make before installing all the gems.

Copy the database config file

The config/database.yml file should not be staged into your version controll system, because it's going to be different for every developer (unless you're using Docker or something similar).

So I first make a copy that will be staged as an example config, and then I edit my own version.

$ cp config/database.yml config/database.yml.example

Don't forget to add config/database.yml to your .gitignore file.

Install the gems

Now it's time to install all the gems.

$ bundle install

Migrate the database

You'll want to run rails db:migrate at this point so it creates your development and test databases.

Install complete

This marks the basic install as complete. But I'm not done setting everything up.

I'll still need to create a repository on Github, and configure my testing tools.

Install RSpec

As a personal preference, I like to use Cucumber and RSpec for testing my Ruby on Rails apps.

First thing is to add RSpec. Add the gem to the development and test environments like so.

group :development, :test do
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'rspec-rails', '~> 3.7'
end

Then, run the bundle command.

$ bundle
$ rails g rspec:install

This last command will generate all the files you need for RSpec to run.

Adding Cucumber

Go to your Gemfile and add these two lines to the test group. Then run the bundle command again.

group :test do
  gem 'cucumber-rails', require: false
  gem 'database_cleaner'
end

You also need to setup Cucumber by generating it's config files.

rails g cucumber:install

What those lines above do is they add the cucumber-rails and database_cleaner gems to your project. DatabaseCleaner is a gem that will help with cleaning our database before each test run so that we always start with nothing in our database. Starting with a clean db before each test is a good thing because we can create our testing environment on the fly and we're able to simulate different scenarios (let me know if you want to read more on the subject).

And to install Cucumber into our project do:

$ bundle install
$ rails generate cucumber:install

Conclusion

This is how I create a Ruby on Rails project from scratch.

Because I use Behavior Driven Development (BDD) on every project, I consider RSpec and Cucumber to be non optional requirements.

12 Project Ideas
Cezar Halmagean
Software development consultant with over a decade of experience in helping growing companies scale large Ruby on Rails applications. Has written about the process of building Ruby on Rails applications in RubyWeekly, SemaphoreCI, and Foundr.