Other Pages

Expand All

Create A Rails App

From here on, this guide assumes you have Rails 7.0.x. To check your Rails version, type this in the terminal:
rails -v
Approximate expected result:
Rails 7.0.x
The greyed-out text may differ and is not important.

If your computer reports a Rails version less than 7.0, ask a TA help get you back on track.

Step 1: Change to your home directory

cd stands for change directory.

If you are still in irb, type quit to exit.

Windows
Type this in the shell:
cd c:\Sites

cd c:\Sites sets our Sites directory to our current directory.

Mac or Linux
Type this in the shell:
cd ~

cd ~ sets our home directory to our current directory.

Step 2: Create a railsbridge directory

Type this in the shell:
mkdir railsbridge

mkdir stands for make directory (folder).

We've made a folder called railsbridge.

Step 3: Change to your new railsbridge directory

Type this in the shell:
cd railsbridge

Step 4: Create a new Rails app

Type this in the shell:
rails new test_app -d sqlite3

The command's output is voluminous, and will take some time to complete, with a long pause in the middle, after all the 'create...' statements ending in 'bundle install'. When it fully completes, it will return you to your home prompt.

Type this in the shell:
cd test_app
Type this in the shell:
rails server

In OS X, you may need to let Ruby accept incoming network connections through your firewall. Select 'allow' in the pop up.

In Windows, you may need to let Ruby and Rails communicate through your firewall. Say yes to the pop up.

Shortcut: Just type 'rails s'

Throughout your Rails programming career you're going to type rails server a lot. Simply typing rails s is the same as rails server.

The first command should produce no output. If rails server starts up with no errors, you're golden! It'll look something like this:

Approximate expected result:
=> Booting Puma
=> Rails 7.0.3 application starting in development
=> Run `bin/rails server --help` for more startup options
Puma starting in single mode...
* Puma version: 5.6.4 (ruby 3.0.3-p157) ("Birdie's Version")
*  Min threads: 5
*  Max threads: 5
*  Environment: development
*          PID: 65783
* Listening on http://127.0.0.1:3000
* Listening on http://[::1]:3000
Use Ctrl-C to stop
The greyed-out text may differ and is not important.

If it does, congratulations! You've successfully installed Ruby AND Rails and started your server.

If it doesn't work, ask a TA for help.

Screenshot of the browser on localhost 3000 showing the rails intro page
  • Don't worry about the details version numbers. You should see ruby 3 and rails 7.
  • Back in the Terminal window where you ran rails server, type Control-C (don't type this into the console, but hold the Control and C keys at the same time) to kill(stop) the server. Windows will ask "Terminate batch job (Y/N)?". Type "Y".

On Windows, sometimes Control-C doesn't work. In that case, look for the key called 'Break' or 'Pause' and press Control-Break, then answer Y at the prompt. If there is no Pause/Break key on your keyboard, you can run ruby script/rails server instead of rails server which should allow Control-C to stop the server.

Step 5: Generate a database model

If your prompt doesn't already show that you are (still) in the test_app folder

Type this in the shell:
cd test_app
Type this in the shell:
rails generate scaffold drink name:string temperature:integer
Type this in the shell:
rails db:migrate
Type this in the shell:
rails server

Note: the above are three separate commands. Type each line into the terminal separately, not as one single command.

Wait until your console shows that the Puma server has started (just like before). Then, in the browser, visit http://localhost:3000/drinks

  1. Click on "New drink"
  2. Enter Cappuccino for the name
  3. Enter 135 for the temperature.
  4. Click on "Create Drink".

(The window where you ran rails server will display debugging information as you do this.)

You should see: Screenshot of the drink detail page

In your terminal, Hold Control and hit C (or on Windows, Control-Break, Y) to stop the rails server.

Next Step: