Creating a Rails Application
In this guide we'll walk through how to create a Rails application completely from scratch and we will also analyze how to look at the web server to perform debugging.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

In this exciting guide, we are going to build out our application. We obviously can't build all the features of our application in one guide, but we are going to generate it in this one. Rails is an amazing framework that allows you to build an entire app with a single command. The syntax for this is: rails new AppName

To get started open your terminal and navigate to the directory where you want to store your application.

Next, type this code: rails new DevcampPortfolio -T --database=postgresql

With this command, we are directing Rails to generate a new application called DevcampPortfolio.

Choosing a Name

While choosing a name for your project, remember not to use spaces. If you have two or more words, you can combine them together in a camel case format like DevcampPortfolio or in a snake case format like devcamp_portfolio. The format is really up to you, as long as you leave no spaces. Rails has a specific naming convention which requires this.

Options

The -T is an option which requests that no test framework is generated. We will not be using the built in testing during the course as we will be using other testing frameworks. Having this built-in testing would only take up more space in our app, and it's best practice not to have extra code you do not need.

With the other option, --database=postgresql, we are requesting that rails uses postgresql as the database for this application. If you don't specify, rails will use the default, SQLite. SQLite is a fine database for certain applications, but postgres will work better for what we want to create.

The Rails New Command

When you run the ‘rails new’ command, the system will create numerous different files for you. Rails will also bring in all kinds of dependencies needed to build out the application. This could take a few minutes depending on your Internet and computer speed.

As you watch the terminal you will see that Rails is creating files and listing where it is placing them within the app. You will see that there are different directories and sub-directories. If there is nothing in front of the file name you will know that those files are placed at the root of your directory. The path app/assets/config/manifest.js will tell you exactly where you will find the manifest.js file.

One interesting file type is .keep. These files are useful when uploading your app to a version control repository like Github. These repositories try to use space efficiently, so if you upload an empty directory, it will automatically remove it. Later, if you want add some files to that directory, it can cause some tricky issues. To avoid all that we will use hidden files like .keep to ensure that all directories show up in the repository, even if they are empty. This way, you don't have to worry about directories disappearing. Now, if that sounded obscure don't worry as we’ll review it later on, it's just good to have a basic understanding of this type of file.

What Rails Built for You

Once your build is complete you need to cd into your app cd DevcampPortfolio (or cd your_app_name)

Now type open . into your terminal.

Now you will be able to see all the directories and files Rails created for you. If you click on the list format in your window you can clearly see the directories and files. You should see an app directory, a bin directory, a config folder, etc...If you are new to rails this may be a little overwhelming. Be assured, one of the points of this course is to guide you through these topics in detail. We will dive into each one of these files and see what their purpose is, how to use them to build applications, and how to use them properly.

The Rails Server

First, let's start the rails server with the command rails s.

While its starting, open your browser and type "localhost:3000".

We may need to create the database first, but let's check out the browser to see if we get an error.

Sure enough, the server has thrown up an error because it doesn't have an associated database.

Like I said before, I'm not going to hide any errors because I want you to know how to fix them.

In this case, there is no database associated with our app, so we'll have to create one, which requires a simple command.

Come back to the terminal and hit ‘CTRL + c’ to stop the rails server.

Creating and Migrating the Database

Next, create the database with the command rails db:create

Once this process is complete, you can see that it created two databases for us called "DevcampPortfolio_development" and "DevcampPortfolio_test".

Next, we'll have to migrate the database, and the command for that is rails db:migrate

A shortcut is to hit the up arrow on your keyboard. This action will bring up the last command you typed into the terminal. You can hit the back arrow to delete the word create and then type the work migrate.

After the migration we need to restart the rails server with the command rails s.

Expect a Yay!!

Now when you go to localhost:3000 in your browser you should see the Rails Welcome Page! The page also notes what Rails version you are on (5.0.1) as well as the Ruby version (2.4.0)

medium

If you go back to the terminal, you'll see the Rails server in action. As you perform tasks on your app, it will be reflected in the terminal. This is true for all sites including big ones like Twitter and Facebook. What you see in the browser window is only the front-end, but there's a background layer running the process behind the scenes and that is the web server.

There is some basic information on the server, like the name of the server and the fact that we went to the homepage. That is reflected in the line 'Started GET "/".'

Just to test, type in something like “localhost:3000/profile" in your browser window. This will throw an error because no such action/page exists. But, if you go to the terminal, you can that see we attempted to go to that page reflected in the line 'Started GET "/profile".' You can also see there was a Routing Error. This may seem messy and a bit confusing right now but as we start building our app this server information will be useful for debugging. Remember, when you get an error you can always go to the terminal to garner information that will help you fix the issue. This will be an important tool as you go on your development journey.

Go back to "localhost:3000" in your browser and we will move on to the next guide where we will start building out our specific features.