In this section, we will walk through the process of creating a simple Ruby on Rails application. By the end of this lesson, you will have a basic understanding of how to set up a Rails project, generate a scaffold, and run your application.
Prerequisites
Before we start, ensure you have:
- Ruby installed on your system.
- Rails gem installed (
gem install rails
). - A basic understanding of Ruby programming.
Steps to Create a Simple Rails Application
- Setting Up a New Rails Project
To create a new Rails application, open your terminal and run the following command:
This command will generate a new Rails project named my_first_app
with a standard directory structure and necessary files.
- Navigating to Your Project Directory
Navigate to your newly created project directory:
- Running the Rails Server
To see your application in action, start the Rails server by running:
By default, the server will run on http://localhost:3000
. Open your web browser and navigate to this URL. You should see the Rails welcome page.
- Generating a Scaffold
Rails scaffolding is a quick way to generate the basic CRUD (Create, Read, Update, Delete) operations for a resource. Let's create a scaffold for a simple Post
resource with a title and body:
This command will generate the following:
- A model file (
app/models/post.rb
) - A migration file to create the
posts
table in the database - Controller files (
app/controllers/posts_controller.rb
) - Views for the CRUD operations (
app/views/posts/
) - Routes for the
Post
resource
- Running Migrations
To apply the generated migration and create the posts
table in your database, run:
- Exploring the Generated Code
Let's take a look at the generated files:
- Model (
app/models/post.rb
): Defines thePost
model. - Controller (
app/controllers/posts_controller.rb
): Contains actions for handling HTTP requests. - Views (
app/views/posts/
): Contains HTML templates for displaying posts. - Routes (
config/routes.rb
): Defines the routes for thePost
resource.
- Accessing the Scaffolded Resource
With the server still running, navigate to http://localhost:3000/posts
in your web browser. You should see a list of posts (which will be empty initially). You can create, view, edit, and delete posts using the generated interface.
- Customizing the Scaffold
You can customize the scaffolded views and controller actions to fit your application's needs. For example, you can modify the form in app/views/posts/_form.html.erb
to include additional fields or change the layout.
Practical Exercise
Task: Create a new Rails application and generate a scaffold for a Book
resource with the following attributes:
title
(string)author
(string)description
(text)
Steps:
- Create a new Rails project named
library
. - Generate a scaffold for the
Book
resource. - Run the migrations.
- Start the Rails server and navigate to
http://localhost:3000/books
to interact with the scaffolded resource.
Solution:
-
Create a new Rails project:
rails new library cd library
-
Generate the scaffold:
rails generate scaffold Book title:string author:string description:text
-
Run the migrations:
rails db:migrate
-
Start the Rails server:
rails server
-
Navigate to
http://localhost:3000/books
in your web browser.
Common Mistakes and Tips
- Missing Migrations: Ensure you run
rails db:migrate
after generating a scaffold to apply the changes to your database. - Server Not Running: If you can't access
http://localhost:3000
, make sure the Rails server is running. - Syntax Errors: Double-check your code for typos or syntax errors, especially when customizing the scaffold.
Conclusion
In this lesson, you learned how to create a simple Rails application, generate a scaffold, and run your application. You now have a basic understanding of how Rails handles CRUD operations and how to customize the generated code. In the next lesson, we will dive deeper into the MVC architecture of Rails and explore how to build more complex applications.
Ruby Programming Course
Module 1: Introduction to Ruby
Module 2: Basic Ruby Concepts
Module 3: Working with Collections
Module 4: Object-Oriented Programming in Ruby
- Classes and Objects
- Instance Variables and Methods
- Class Variables and Methods
- Inheritance
- Modules and Mixins
Module 5: Advanced Ruby Concepts
Module 6: Ruby on Rails Introduction
- What is Ruby on Rails?
- Setting Up Rails Environment
- Creating a Simple Rails Application
- MVC Architecture
- Routing
Module 7: Testing in Ruby
- Introduction to Testing
- Unit Testing with Minitest
- Behavior-Driven Development with RSpec
- Mocking and Stubbing