Haroon Ahmed

Your new rails application

01 Jun 2020

For this post, the latest commit in rails master is 0267f18.

Ready to create your new rails application?

There is a lot of frameworks included by default. I’m going to explain what happens when you run the rails new app_name command to create a new application. What is included and why.

rails new

To create a new rails application first thing we need to do is install the rails gem. The rails gem helps us create our rails application.

Ensure you have ruby and bundler installed on your machine.

gem install rails

Now we can go ahead and create our rails application. Let’s use the command:

rails new cool_app

This will generate a new rails application with a lot of default frameworks included. The most important frameworks are listed:

  • sqlite3 your database where you will store your data
  • action cable integrates web sockets with your rails application
  • action mailer framework for designing email services
  • action mailbox framework to handle incoming emails
  • active record Object Relational Mapper (ORM) which interfaces with your chosen database
  • active storage file storage solution using cloud services (S3, Azure)
  • action text rich text content and editing using the trix editor
  • sprockets Asset management framework for css and javascript
  • turbolinks makes navigating your web application faster
  • webpacker handles your app-like javascript modules, particulalary important when you use react/vue/stimulusjs for your front end code

Other noteworthy defaults that deserve a mention:

  • puma ruby web server built for speed and parallelism
  • bootsnap boots rails apps, faster
  • spring speeds up rails developement by keeping your app running in the background

Sqlite3 is not ideal for production. It is mostly used for prototyping and should not be used in production. I find it useful to go with my preferred database, postgresql. Mysql is another popular choice, used by Basecamp, GitHub and others.

Both mysql and postgresql can be in all environments (development/staging/production).

Let’s add the database flag to the rails command to use our preferred database:

rails new cool_app -d=postgresql

There is one final thing that we should consider, do we need webpacker or will sprockets handle our javascript too?

Sprockets is very good for handling all css and images. It was also originally used to drop in javascript files and it would handle bundling them into one file. This approach is still preferred amongst some developers. If that suits you better, skip webpacker entirely with the flag --skip-webpack.

However it’s worth mentioning, webpacker is the preferred option to handle javascript. There is a number of front end frameworks out there like react, vue, angular js, choose your preferred framework. You also have the option of not using a framework too.

Personally I prefer a minimal front end framework like stimulusjs. Let’s go ahead and update our command to include this flag:

rails new cool_app -d=postgresql --webpack=stimilusjs

At this point we are good to go. However, I briefly want to mention we can exclude a framework that rails includes in our app that we don’t need, for example we can exclude ActionMailer.

NOTE: If you are new to rails, it’s far easier having all the default frameworks. If there is a framework you seldom use, it’s not going to do any harm keeping it. Also, it’s trivial to remove a framework later.

For aguments sake, let’s go ahead and remove a few frameworks.

Websocket support is really nice, however not every application wants to add this right away, so let’s drop action cable.

Let’s say for this new app, we are not going to parse incoming emails and convert into a model object. That means action mailbox is dropped too.

That’s 2 frameworks in total we want to skip, let’s take a look at our updated rails command:

rails new cool_app -d=postgresql --webpack=stimilusjs --skip-action-mailbox --skip-action-cable 

That’s a whole load of flags to remember!

Let’s improve this by adding a .railsrc file that is automitcally used when we create our rails application.

touch ~/.railsrc

Then add the all our preferred flags:

echo "-d=postgresql --webpack=stimilusjs --skip-action-mailbox --skip-action-cable" >> ~/.railsrc

The next time we create a rails app, all the default flags we added to .railsrc will be used and we not longer have to remember all the flags:

rails new yet_another_cool_app

api only

Sometimes we may want to hack on an api only app. We don’t want webpacker or sprockets. We can easily do that with the command:

rails new cool_api --api

Personally, I end up needing a rails app alongside an api. Therefore I end up creating a normal rails app, then add the api application controller:

# app/controllers/api/application_controller.rb
class ApplicationController < ActionController::API
end

Then I add my api specific controllers. for example: app/controllers/api/shops_controller.rb.

# app/controllers/api/shops_controller.rb
class ShopsController < ApplicationController
  def index
    # do stuff
  end
end

I’m a minimalist

A minimal stack means a bare bones rails application, with the database, active record and literally nothing else. So we exclude:

  • active_storage
  • action_cable
  • active_job
  • action_mailbox
  • action_mailer
  • action_text
  • bootsnap
  • jbuilder
  • spring
  • system_tests
  • turbolinks
  • webpack

We could do this with the command below, however there is far too many flags that need to be added.

rails new cool_app -d=postgresql --skip-webpack --skip-action-mailbox --skip-action-cable --skip-turbolinks --skip-bootsnap --skip-spring ... 

It would be nice if we could use a flag which removed everything by default.

rails new cool_app -d=postgresql --minimal

I have opened a pull request for this very feature. See here for more details: https://github.com/rails/rails/pull/39282

hacking on rails

When hacking on rails, we also want to create a new rails application using the latest code we have checked out. This was particularly useful for me when I was working on some features which I needed access to.

Firstly, cd into the directory where you pulled down your rails code, for example:

cd projects/rails

Make your changes, then run the command in the directory above:

rake install

This will install the latest rails gem on your machine. You may have to uninstall the current installed rails version gem uninstall rails before running the command above.

We can verify the current rails gem installed by checking its version.

rails --version
> Rails 6.1.0.alpha

Now we have the latest version installed, we can use the command below to create a new rails app with the updated changes.

rails new latest_cool_app

Alternatively we can use the rails command directly to create a new rails app. Which saves us the trouble installing the latest rails gem.

railties/exe/rails new ~/Desktop/testapps/app_name

This post is part of a series of posts I intend to do about rails. Hope you found it useful.