How I Start Rails Projects v2

2 minute read

An update to my somewhat popular How I Start Rails Projects post. As before, I start with:

rvm use 2.4.1@project-name --create
gem install rails
rails new project-name
cd project-name
rvm use 2.4.1@project-name --ruby-version
echo '/.ruby-*' >> .gitignore

The only change is bumping Ruby to 2.4.1.

But this leads to a rant:

When I posted the original version, some troll commented something along the lines of:

“You lost me when you said rvm.”

Dude (of any, all, or no gender), you are an idiot. Not because you don’t like RVM, but because it doesn’t matter that you don’t like RVM and you are wasting your time hating on it.

I’m not going to defend RVM because:

a. You can google and find plenty of people explaining why RVM is wrong and plenty of other people explaining why people who think RVM is wrong are wrong.

b. I don’t care. I don’t care for the simple reason RVM works for me. I use RVM because that it was the only option at the time. I have nothing against rbenv, I’ve used it on servers. However, I’m not going to spend a day ripping out something that’s working fine for me to replace it with something that’s technically, but not actually functionally, better for me. Use whatever you like, but don’t be a jerk by telling people their tools are somehow inferior to yours.

Back to the task at hand. I edit the Gemfile, first adding:

ruby '2.4.1'

to the top allowing Heroku and RVM to automatically pickup the Ruby version (rbenv doesn’t do this out of the box, but there is a plugin).

Then I move:

# Use sqlite3 as the database for Active Record
gem 'sqlite3'

in to the test and development group:

group :development, :test do
  # Use sqlite3 as the database for Active Record in development
  gem 'sqlite3'
  # [...]
end

sqlite3 is fine, preferable even, for development, but in production you need something more rubust. I create a new production group:

group :production do
  # Use PostgreSQL as the database for Active Record
  gem 'pg' # or gem 'mysql2'
end

I add two development gems I like:

group :development, :test do
  [...]
  gem 'dotenv-rails'
  gem 'awesome_print'

The dotenv gem allows you to manage project specific environmental variables by storing them in a .env file, which we need to ignore:

echo '/.env' >> .gitignore

Awesome Print is a “ruby object pretty printer”. It add an ap method which nicely formats and syntax highlights ruby objects.

You can read more about dotenv and Awesome Print in the original post.

The change here is I’ve stopped using Pry via pry-byebug, I’ve become accustom to Byebug which ships with Rails.

Finally, if it’s a user facing project, as opposed to an API, and it doesn’t have it’s own styles, I bring in Bootstrap:

gem 'bootstrap-sass'

and replace the contents of app/views/layouts/application.html.erb with:

<!DOCTYPE html>
<html>
  <head>
    <title>My Cool App</title>
    <%= csrf_meta_tags %>

    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
  </head>

  <body>
      <div id="wrapper" class="container-fluid">
          <%= yield %>
      </div>
  </body>
</html>

And then it’s just:

git add .
git commit -m 'Blank Slate'

Starting with Rails 5.1.1, rails new does a git init unless you use the --skip-git option, so that step is no longer necessary.

As always, this is what works for me, but I think it’s a good starting point to think about what works for you.

Comments