Getting Started from Scratch
Throughout the tutorial, you’re going to run a lot of commands on the command line. For example, we might ask you to do something like
Run
pwd
to print your current working directory.
Where you run these commands will vary depending on the type of computer you’re using.
-
If you’re using a Mac, you’ll type the command into Terminal.
-
If you’re on Windows, you’ll type the command into Command Prompt.
After you’ve typed the command, you’ll press enter
to execute it.
To keep things simple, we’ll refer to both Terminal and Command Prompt as Terminal in the tutorial.
Windows users! If you followed our install instructions, you’ll have to run C:\RubyDevKit\devkitvars.bat
when you open the Command Prompt.
-
Get started by opening Terminal.
-
You’re now in your home directory. Let’s take a look what you have in there.
Run
ls
to list everything inside your home directory.ls
You might see some familiar directories like
Desktop
,Documents
, andMusic
. -
All code deserves a good home. Add a new
Projects
directory to your home directory by runningmkdir Projects
.mkdir Projects
-
Navigate into your new
Projects
directory by runningcd Projects
.cd Projects
Now you’re ready to create a new Rails application!
Before you can continue with the tutorial, your computer needs to be configured with Rails. If you haven’t already, please refer to the install instructions to get everything set up and ready to go.
-
Run
rails new bookstore
to create a new Rails application named “bookstore”.rails new bookstore
-
After
rails new
finishes, runcd bookstore
to navigate into the newly created bookstore application.cd bookstore
Woah! The rails new
command does a lot of stuff!
When you run rails new
, it creates the files and directories that make up the basic structure of your new Rails application. Then, it runs bundle install
to install the dependencies needed to run the application.
Let’s take a look the files that got created when you ran rails new
.
-
Run
ls -l
to list out all the top level files and directories in thebookstore
directory.ls -l
There’s a lot of stuff in there, but a few things stand out.
-
Any code that is specific to your bookstore application will live in the
app
directory. This is where you’ll be doing most of your work for this tutorial.Run
ls -l app
and take a look at some of things in there.ls -l app
It’s easiest to view and edit the files of your application using a text editor created just for programming. If you don’t already have a favorite text editor we recommend Atom.
The config
directory is where you’ll store configuration files for the bookstore application.
rails new
generated a bunch of configuration files to help you get this new application up and running quickly. Let’s take a look at one of those files.
-
Using your favorite text editor, open the whole
bookstore
directory.If you’re using Atom, you can do this by going to
File
>Open...
. -
Now, open
config/database.yml
.database.yml
tells Rails how to connect to the bookstore application’s database.By default, the bookstore application is setup to use SQLite. It’s a light weight database that’s perfect for this tutorial.
Since we’re happy with the default
database.yml
generated byrails new
, we can live it as is.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: sqlite3
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
Not sure what a database is? Don’t worry, we’ll fill you in. 😊
There are many different types of databases out there, but we’ll only be working with a relational database.
A relational database is made up of tables where you store data. You can think of them kinda like an Excel spreadsheet but much more powerful. There are many types of Relational Database Management Systems (RDMS’s for short) and SQLite is just one of them.
Feel free to check out this video to learn a bit more about Relational Databases:
While you’re working through the tutorial, don’t worry if what you’re seeing doesn’t look exactly like the examples in the tutorial. Everyone’s setup is going to be a little different, and small things can change between different versions of Rails.
Let’s get the application running!
-
Go back to Terminal and make sure you’re in the
bookstore
directory by runningpwd
.pwd
pwd
shows you what directory you’re in. You should see something like/Users/awesomesauce/Projects/bookstore
. -
If you’re not in the
bookstore
directory, you’re probably in your home directory.To get to the
bookstore
directory from your home directory, first runcd Projects
to get to yourProjects
directory. Then, runcd bookstore
.cd Projects cd bookstore
-
Now run
rails server
to start your application’s web server.If you take a close look at the command’s output, you’ll see where you can access the running application:
=> Rails 5.0.0.1 application starting in development on http://localhost:3000
-
Open you web browser and go to http://localhost:3000.
Yay! The bookstore application is running!
-
Go back to Terminal and take a look at what’s happening in your application’s web server. You can see the application responding to requests!
-
Now that you’re back in Terminal, run
Ctrl-C
to stop your application’s web server.
What’s this bookstore we keep talking about?
In this tutorial, you’re going to use Rails to build an application for a bookstore.
As you build the application, you’ll add features to create, view, edit, and delete books. Since we all have feelings on the books we love…and hate, you’ll add features so books can be reviewed by users.
By creating an application for a bookstore, you’ll get to explore the different layers of the Rails framework.
Are you ready?!