
🚀 Introduction
If you’re looking for a beginner-friendly Ruby on Rails tutorial, there’s no better way to learn than by building a simple CRUD (Create, Read, Update, Delete) app. CRUD operations are the foundation of most applications — whether you’re managing users, blog posts, or tasks.
In this guide, we’ll build a basic blog app using Rails from scratch. It’s a practical, fast, and beginner-friendly way to understand how Rails handles data and views.
🧱 Prerequisites
Before we begin, make sure you have:
- Ruby (via RVM or rbenv)
- Rails installed (
gem install rails
) - SQLite or PostgreSQL installed
- Basic terminal & text editor knowledge
1️⃣ Ruby on Rails Tutorial: Create a New App
rails new blog_app --skip-javascript --database=sqlite3
cd blog_app
--skip-javascript
is optional if you’re not integrating a JS framework yet.
2️⃣ Ruby on Rails Tutorial: Generate a Post Model
We’ll create a Post
model with two fields: title
and body
.
rails generate scaffold Post title:string body:text
Now run:
rails db:migrate
This sets up your database table and adds the full CRUD structure.
3️⃣ Ruby on Rails Tutorial: Start the Development Server
rails server
Open http://localhost:3000/posts to see your working CRUD app!
4️⃣ Understanding CRUD in This Ruby on Rails Tutorial
Thanks to the scaffold, your app supports:
Action | Route | Purpose |
---|---|---|
GET /posts | index | View all posts |
GET /posts/new | new | Form to create |
POST /posts | create | Save new post |
GET /posts/:id | show | View single post |
GET /posts/:id/edit | edit | Edit form |
PATCH /posts/:id | update | Update post |
DELETE /posts/:id | destroy | Delete post |
5️⃣ Ruby on Rails Tutorial: Customize the View
Let’s improve the post listing in app/views/posts/index.html.erb
:
<h1>All Posts</h1>
<%= link_to 'Create New Post', new_post_path, class: "btn btn-primary" %>
<% @posts.each do |post| %>
<div class="post-card">
<h2><%= link_to post.title, post %></h2>
<p><%= truncate(post.body, length: 150) %></p>
<%= link_to 'Edit', edit_post_path(post) %> |
<%= link_to 'Delete', post, method: :delete, data: { confirm: 'Are you sure?' } %>
</div>
<% end %>
6️⃣ Ruby on Rails Tutorial: Add Model Validations
In app/models/post.rb
, add:
class Post < ApplicationRecord
validates :title, presence: true
validates :body, presence: true, length: { minimum: 10 }
end
This prevents empty posts from being saved.
7️⃣ Ruby on Rails Tutorial: Improve App Navigation
Update app/views/layouts/application.html.erb
:
<nav>
<%= link_to 'Home', root_path %> |
<%= link_to 'Posts', posts_path %>
</nav>
Make sure root
is set in routes.rb
:
root 'posts#index'
🔄 Final Touch:
Before you go further with more advanced features, it’s important to keep your app updated and secure.
👉 Read why upgrading Ruby on Rails is essential
🧠 Recap
You now have a fully working Rails CRUD app. Here’s what you’ve learned:
- Scaffold generation in Rails
- How routing, models, and views interact
- Validations and basic layout edits
🚀 Next Steps
- Add authentication using Devise
- Style your app with TailwindCSS or Bootstrap
- Deploy it to Render or Heroku