Ruby on Rails - Examples
Ruby on Rails - Examples
Section titled “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’.
Workflow for Creating Rails Applications
Section titled “Workflow for Creating Rails Applications”A typical workflow for developing a Rails application involves these steps:
- Initialize Application: Use the
rails newcommand 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.rbto 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.
Creating a New Rails Web Application
Section titled “Creating a New Rails Web 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.
- Open your terminal or command prompt.
- Navigate to the directory where you want to create your application.
- Run the following command to create a new Rails application named
library_system:
$ rails new library_systemThis 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.
Starting the Web Server
Section titled “Starting the Web Server”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:
- Navigate into your application directory:
cd library_system - Run the Rails server command:
bin/rails server(orrails sfor short).
$ cd library_system$ bin/rails serverYou 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 optionsPuma 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]:3000Use Ctrl-C to stopThis 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.
What is next?
Section titled “What is next?”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.