How to Create Your First Ruby on Rails Application

In this lesson, you will learn how to create your first Ruby on Rails application.

How to Generate a Rails Application Using rails new

To start, use your terminal navigate to the folder that you'd like to create your Rails application in. For me, that's my Dev folder. I can navigate to this directory using the following statement;

cd Dev

Once you have navigated to the desired directory, it is time to generate a boilerplate Rails application.

You can generate the Rails application with the rails new command followed by the name of your application. The application name cannot contain spaces. In my case, I will name my application rails_course.

We will also pass in a few optional parameters to our rails new statement. Specifically, we will add:

  • -T - this tells Rails to avoid installing its test framework, which takes up unnecessary space since we will not be using it in this course
  • --database=postgresql - without this parameter, Rails will use the SQLite database, a smaller, less feature-rich database than the PostgreSQL database that we installed in the last lesson

Here's what the full statement looks like:

rails new rails_course -T --database=postgresql

After you run this statement, Rails will create dozens of files and install all of the dependencies it requires. This is likely to take several minutes.

Your terminal may print the following statement after running the rails new command:

Run `bundle install` to install missing gems.

If this is the case, then navigate into your new application's directory with cd rails_course and run the bundle install command to install the gems that you are missing.

You can now open the directory in using your Mac's Finder application with the open . statement. You'll see many folders that have just been created using the rails new command, including app, bin, and config.

How to Start a Rails Server Using rails s

Now that we have created our boilerplate Rails application, we need to start a Rails server to actually view the application.

We will need to install yarn to be able to start this server. We can install yarn using Homebrew with the following command:

brew install yarn

Now that this is done, You can start a rails server with the following command:

rails s

Note that if you receive any errors while running the rails s command, then the error message will contain instructions on how to fix the error.

As an example, I originally encountered an error while running this on my new machine. The error message contained the following excerpt: Please run rails webpacker:install. Accordingly, I fixed the error by running the rails webpacker:install command.

If your rails s command is successful, then your terminal should print a few lines of text that end in Use CTRL+C to stop. This means that your server is now running, and you can view your application by typing the URL [http://127.0.0.1:3000/](http://127.0.0.1:3000/) into your browser.


Written on