Rails Server and Console

Two tools keep you productive during Rails development: the server and the console. The server runs your app in the browser. The console lets you interact with your app directly from the terminal without opening a browser.

The Rails Server

The server starts your application so a browser can access it. Rails uses a web server called Puma by default.

rails server
  or
rails s

What Happens When You Start the Server

You run: rails server
        |
        v
Puma starts up
        |
        v
Rails loads your app code
        |
        v
Server listens at: http://localhost:3000
        |
        v
Browser visits the URL
        |
        v
Rails handles the request → sends back a response

Reading Server Logs

When your server runs, every browser request prints a log entry. Understanding this log helps you debug problems quickly.

Started GET "/" for 127.0.0.1 at 2024-01-10 10:05:23
Processing by PagesController#home as HTML
  Rendered pages/home.html.erb (Duration: 1.2ms)
Completed 200 OK in 15ms

The log shows: what request came in, which controller handled it, which view was rendered, and how long it took. A 200 status means success. A 404 means the page was not found. A 500 means your code has an error.

Run the Server on a Different Port

By default, the server uses port 3000. If another app uses that port, start Rails on a different one:

rails s -p 4000

Then visit http://localhost:4000.

Run the Server and Bind to All Interfaces

If you run Rails on a remote machine (like a cloud server) and want to access it from your local browser:

rails s -b 0.0.0.0

The Rails Console

The Rails console is a powerful interactive tool. It loads your entire application and lets you run Ruby and Rails code line by line. Use it to test code, inspect data, and debug problems.

rails console
  or
rails c

Your terminal prompt changes to show you are inside the console:

Loading development environment (Rails 7.1.0)
irb(main):001>

Console Is Like a Conversation With Your App

You type a command
        |
        v
Rails runs it immediately
        |
        v
Result appears on screen
        |
        v
You type the next command

Common Console Commands

Here are the things you do most often in the console:

Check How Many Records Exist
User.count
# => 5
Find All Records
User.all
# => Returns an array of all users
Find a Specific Record
User.find(1)
# => Returns the user with ID = 1
Create a New Record
User.create(name: "Alice", email: "alice@example.com")
# => Creates and saves the user to the database
Update a Record
user = User.find(1)
user.update(name: "Alice Smith")
# => Updates the name in the database
Delete a Record
user = User.find(1)
user.destroy
# => Removes the record from the database

Test Your Model Logic in the Console

The console is perfect for testing model methods before adding them to your app. For example, if you add a method full_name to your User model:

# app/models/user.rb
def full_name
  "#{first_name} #{last_name}"
end

Test it in the console:

user = User.find(1)
user.full_name
# => "Alice Smith"

Reload the Console

When you change code in your app, the console does not automatically pick up those changes. Reload it with:

reload!

This refreshes your app code without exiting the console session.

Exit the Console

exit
  or press Ctrl + D

The Sandbox Console

The sandbox console lets you experiment without permanently changing your database. Every change you make gets rolled back when you exit.

rails console --sandbox

Use this when you want to test database operations but do not want to actually save anything.

Rails Generator Commands

Besides the server and console, you frequently use Rails generators. These create files for you so you do not have to write boilerplate code manually.

CommandWhat It Creates
rails g controller PostsA controller file, view folder, and route
rails g model Post title:string body:textA model file and a database migration
rails g scaffold Post title:string body:textA complete set of CRUD files for Post
rails g migration AddEmailToUsers email:stringA migration file to add a column

Rails Routes Command

Use this command to see every URL route your app currently has:

rails routes

Output looks like this:

Prefix    Verb    URI Pattern        Controller#Action
root      GET     /                  pages#home
posts     GET     /posts             posts#index
          POST    /posts             posts#create
new_post  GET     /posts/new         posts#new
edit_post GET     /posts/:id/edit    posts#edit
post      GET     /posts/:id         posts#show
          PATCH   /posts/:id         posts#update
          DELETE  /posts/:id         posts#destroy

This output helps you confirm that your routes are set up correctly and shows you the URL pattern for each controller action.

The Server and Console Work Together

A common workflow during development looks like this:

Terminal 1: rails s        ← server running, watching browser
Terminal 2: rails c        ← console open, testing data
Editor: your code files    ← writing and editing code

Keep both terminals open at the same time. Use the server to see how your changes look in the browser. Use the console to test data logic without the overhead of clicking through the UI.

Leave a Comment

Your email address will not be published. Required fields are marked *