Creating a New 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 (in Where Things Go), 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 open 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
 create README.md
 create Rakefile
 create .ruby-version
  : : :
 remove config/initializers/cors.rb
 remove config/initializers/new_framework_defaults_5_1.rb
  run bundle install
 Fetching gem metadata from https://rubygems.org/...........
  : : :
 Webpacker successfully installed
 work>

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
 app/ db/ node_modules/ README.md yarn.lock
 babel.config.js Gemfile package.json storage/
 bin/ Gemfile.lock postcss.config.js test/
 config/ lib/ public/ tmp/
 config.ru log/ Rakefile vendor/

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 ails​​ ​​about

If you get a Rails version other than 6.0.1, reread Choosing a Rails Version.

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,[29] and starting over. If that doesn’t work, use bundle exec[30] 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
 => Booting Puma
 => Rails 6.0.1 application starting in development
 => Run `rails server --help` for more startup options
 Puma starting in single mode...
 * Version 3.12.1 (ruby 2.6.2-p47), codename: Llamas in Pajamas
 * Min threads: 5, max threads: 5
 * Environment: development
 * Listening on tcp://localhost:3000
 Use Ctrl-C to stop

Note 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 pointing a browser at the URL http://localhost:3000. The result is shown in the screenshot.

images/demo2_1_hello_rails.png

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 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. Let’s rectify this situation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.183.89