A Guide to Scaffolding in Ruby on Rails

Ashvin Choudhary
3 min readJun 11, 2023

Scaffolding is a powerful tool in Ruby on Rails that allows developers to quickly generate the basic components of a web application, such as models, views, and controllers. It’s a time-saving technique that automates repetitive tasks, helping you get started on your application faster. In this blog post, we’ll explore the concept of scaffolding in Ruby on Rails and walk through code examples to showcase its capabilities.

What is Scaffolding?
Scaffolding in Ruby on Rails is a code generation technique that creates a set of files and code templates based on a predefined set of rules. It provides a starting point for building CRUD (Create, Read, Update, Delete) functionality, allowing developers to focus on application-specific logic rather than writing boilerplate code.

Creating a Scaffold:
To create a scaffold in Ruby on Rails, you can use the `rails generate scaffold` command followed by the desired model name and its attributes. Let’s say we want to create a scaffold for a simple blog post model with attributes like title, content, and author.

rails generate scaffold Post title:string content:text author:string

This command will generate a series of files and directories, including a model, controller, views, and database migrations. It also creates RESTful routes for CRUD operations.

Migrations:
Scaffolding automatically generates a database migration file that sets up the necessary database schema. You can apply the migration using the `rails db:migrate` command. Here’s an example of a migration generated for the `Post` model:

class CreatePosts < ActiveRecord::Migration[6.1]
def change
create_table :posts do |t|
t.string :title
t.text :content
t.string :author
t.timestamps
end
end
end

This migration creates a `posts` table in the database with the specified columns and timestamps for tracking creation and modification dates.

Controllers:
Scaffolding also generates a controller that handles CRUD operations for the model. The controller contains methods for handling requests and rendering views. Here’s an example of the `PostsController` generated for our scaffold:

class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]

def index
@posts = Post.all
end

def show
end

def new
@post = Post.new
end

def edit
end

def create
@post = Post.new(post_params)

if @post.save
redirect_to @post, notice: 'Post was successfully created.'
else
render :new
end
end

def update
if @post.update(post_params)
redirect_to @post, notice: 'Post was successfully updated.'
else
render :edit
end
end

def destroy
@post.destroy
redirect_to posts_url, notice: 'Post was successfully destroyed.'
end

private

def set_post
@post = Post.find(params[:id])
end

def post_params
params.require(:post).permit(:title, :content, :author)
end
end

The controller actions correspond to the CRUD operations, providing the necessary logic for handling requests and rendering views.

Views:
Scaffolding generates views for each action in the controller. These views define the HTML templates that display the data to the user. For example, the `index.html.erb` view might look like this:

<h1>Posts</h1>

<table>
<thead>
<tr>
<th>Title</th>
<th>Content</th>
<th>Author</th>
<th colspan=”3"></th>
</tr>
</thead>

<tbody>
<% @posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.content %></td>
<td><%= post.author %></td>
<td><%= link_to ‘Show’, post %></td>
<td><%= link_to ‘Edit’, edit_post_path(post) %></td>
<td><%= link_to ‘Destroy’, post, method: :delete, data: { confirm: ‘Are you sure?’ } %></td>
</tr>
<% end %>
</tbody>
</table>

<br>

<%= link_to ‘New Post’, new_post_path %>

This is just a sample view, but it gives you an idea of how scaffolding generates HTML templates with dynamic content.

Conclusion:
Scaffolding in Ruby on Rails is an efficient way to kickstart your application development process. It saves you time by generating the boilerplate code necessary for CRUD operations, allowing you to focus on building the core features of your application. By following the examples and understanding how scaffolding works, you can accelerate your Ruby on Rails development process and create robust web applications more efficiently.

--

--

Ashvin Choudhary

Ashvin Choudhary is an AWS Certified Senior Software Engineer with 8+ years of experience. He's enthusiastic about tech and believes in lifelong learning.