Learn to create a new Rails application
We'll cover the following
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. 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.
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 live terminal
Note: You can run the above commands that are discussed so far by using the below terminal:
Note: If you look at the terminal where you started the server, you can see tracing showing that you started the application. When 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.
A hello world application
We have provided the files that you have generated so far, using the above commands. You can run the simple hello world application below.