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.
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:
Other noteworthy defaults that deserve a mention:
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
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.
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
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 class ShopsController < ApplicationController def index # do stuff end end
A minimal stack means a bare bones rails application, with the database, active record and literally nothing else. So we exclude:
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
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:
Make your changes, then run the command in the directory above:
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.