Skip to content

Ruby on Rails - Examples

In this chapter, we’ll walk through creating a simple online library system. This application will manage books and the subjects they belong to, demonstrating core Rails concepts.

Our application will use two main data models:

  • Book: Represents an individual book with attributes like title, price, description, and a subject.
  • Subject: Represents a category for books, like ‘Physics’ or ‘History’.

A typical workflow for developing a Rails application involves these steps:

  • Initialize Application: Use the rails new command to generate the application skeleton.
  • Database Setup: Create a database (e.g., PostgreSQL, MySQL, or use the default SQLite) and configure config/database.yml.
  • Model Creation: Define Active Record models (app/models/) to represent your application’s data and their relationships.
  • Migrations: Generate and run database migrations (db/migrate/) to create or modify your database schema (tables, columns).
  • Routing: Define routes in config/routes.rb to map URLs to controller actions.
  • Controller Logic: Write controller actions (app/controllers/) to handle requests, interact with models, and prepare data for views.
  • View Templates: Create view templates (app/views/) using ERB (or other templating engines) to display data and provide user interfaces.
  • Testing: Write tests (unit, integration, system) to ensure your application works as expected.

Let’s begin building our library application.

Rails provides command-line generators to automate many common development tasks. We’ll use rails new to create the basic structure for our library system.

  1. Open your terminal or command prompt.
  2. Navigate to the directory where you want to create your application.
  3. Run the following command to create a new Rails application named library_system:
$ rails new library_system

This command creates a library_system directory containing a complete Rails application structure. For details on this structure, refer to the ‘Rails Directory Structure’ chapter.

Key directories for development within library_system/app/ include:

  • controllers/: Contains controller classes that handle web requests.
  • models/: Holds Active Record model classes that interact with the database.
  • views/: Stores view templates for rendering HTML.
  • helpers/: Contains helper modules to assist views and controllers.
  • assets/: Manages static assets like stylesheets, JavaScript files, and images.
  • jobs/: For background jobs using Active Job.
  • mailers/: For email handling with Action Mailer.

Rails applications can run on various web servers. For development, Rails comes with the Puma web server, which is easy to start.

To start the development server:

  1. Navigate into your application directory: cd library_system
  2. Run the Rails server command: bin/rails server (or rails s for short).
$ cd library_system
$ bin/rails server

You should see output similar to this, indicating Puma has started:

=> Booting Puma
=> Rails 7.x.y application starting in development
=> Run `bin/rails server --help` for more startup options
Puma starting in single mode...
* Puma version: 6.x.y (ruby 3.x.y-p0) ("The Eagle of Durango")
* Min threads: 5
* Max threads: 5
* Environment: development
* PID: 12345
* Listening on http://127.0.0.1:3000
* Listening on http://[::1]:3000
Use Ctrl-C to stop

This starts the Puma server, typically on port 3000.

Now, open a web browser and navigate to http://localhost:3000 (or http://127.0.0.1:3000). If everything is set up correctly, you should see the default Rails welcome page. The appearance of this page varies slightly between Rails versions, but it generally indicates your application is running.

The next step is to set up the database for our application and configure Rails to connect to it. We’ll then define our models and create the database schema using migrations.