Learn to create a new Rails application

When you install the Rails framework, you also get a new command-line tool, rails, that’s used to construct each new Rails application you write.

Why do we need a tool to do this? Why can’t we just hack away in our favorite editor and create the source for our application from scratch? Well, we could just hack. After all, a Rails application is just Ruby source code. But Rails also does a lot of magic behind the curtain to get our applications to work with a minimum of explicit configuration. To get this magic to work, Rails needs to find all the various components of your application. As you’ll see later, this means we need to create a specific directory structure, slotting the code we write into the appropriate places. The rails command creates this directory structure for us and populates it with some standard Rails code.

To create your first Rails application, pop opens a shell window and navigate to a place in your filesystem where you want to create your application’s directory structure. In our example, we’ll be creating our projects in a directory called work. In that directory, use the rails command to create an application called demo. Be slightly careful here—if you have an existing directory called demo, you’ll be asked if you want to overwrite any existing files.

Note: if you want to specify which Rails version to use, as described in Choosing a Rails Version, now is the time to do so.

rubys> cd work
work> rails new demo create

The command has created a directory named demo. Pop down into that directory and list its contents (using ls on a Unix box or using dir on Windows). You should see a bunch of files and subdirectories:

work> cd demo
demo> ls -p

All these directories (and the files they contain) can be intimidating to start with, but you can ignore most of them for now. In this chapter, we’ll only use two of them directly: the bin directory, where we’ll find the Rails executables; and the app directory, where we’ll write our application.

Examine your installation using the following command:

demo> bin/rails about

Windows users need to prefix the command with ruby and use a backslash:

demo> ruby bin\rails about

This command also detects common installation errors. For example, if it can’t find a JavaScript runtime, it provides you with a link to available runtimes.

As you can see from the bin/prefix, this is running the rails command from the bin directory. This command is a wrapper, or binstub, for the Rails executable. It serves two purposes: it ensures that you’re running with the correct version of every dependency, and it speeds up the startup times of Rails commands by preloading your application.

If you see a bunch of messages concerning already initialized constants or a possible conflict with an extension, consider deleting the demo directory, creating a separate RVM gemset, and starting over. If that doesn’t work, use bundle exec2 to run rails commands:

demo> bundle exec rails about

Once you get bin/rails about working, you have everything you need to start a stand-alone web server that can run our newly created Rails application. So, without further ado, let’s start our demo application:

demo> bin/rails server

if you are using a virtual machine, you need to run Rails like so:

demo> bin/rails server -b 0.0.0.0

As the second line of the startup tracing indicates, we started a web server on port 3000. The localhost part of the address means that the Puma web server will only accept requests that originate from your machine. We can access the application by clicking the RUN button to below application.

If you look at the window where you started the server, you can see tracing showing that you started the application. We’re going to leave the server running in this console window. Later, as we write application code and run it via our browser, we’ll be able to use this console window to trace the incoming requests. When the time comes to shut down your application, you can press Ctrl-C in this window to stop the server. (Don’t do that yet—we’ll be using this particular application in a minute.)

If you want to enable this server to be accessed by other machines on your network, you will either need to list each server you want to have access to separately or you can enable everybody to access your development server by adding the following to config/environments/development.rb:

config.hosts.clear

You will also need to specify 0.0.0.0 as the host to bind to the following code:

demo> bin/rails server -b 0.0.0.0

At this point, we have a new application running, but it has none of our code in it.

A running application

You can find the working example of Rails below, which we have discussed above.

Click on Run button to see the app running
Hello world Rails application

Anatomy of a Rails project

The table below provides a summary of the standard file and directory structure of a Rails application. The information provided below will allow you to get your bearings so you can start working on your project.

File/Folder Purpose
app/ Contains your core application code: controllers, models, views, helpers, mailers, channels, jobs, and assets for your application
bin/ Contains binary executable files (scripts) to start, update, deploy, or run your application
config/ Contains configuration files for the application, routes, database, etc.
db/ Contains database files, your current database schema, and database migrations
lib/ Contains extended library modules for your application.
log/ Contains application log files
public/ Contains data that is accessible via browsers, i.e., static files, compiled assets, error pages, etc.
storage/ Contains active storage files –– more on this later
test/ Contains application unit tests, fixtures, and various other test apparatus
tmp/ Contains temporary files like cache and pid files.
vendor/ Contains all third party code
Gemfile This file contains the gem requirements for the application, which are Ruby packages needed to run the app.
Gemfile.lock This file contains a list of gems to ensure all copies of the application use the same version
Rakefile This file allows the command line to locate and run rake tasks, which is a Make-like build utility implemented in Ruby, and allows for automation of tasks
config.ru This file contains configuration for the Rack middleware based servers used to start the application
package.json This file allows you to specify any npm dependencies that are needed for your Rails application

Get hands-on with 1400+ tech skills courses.