Rails Generators: Scaffold vs Resource

"rails g resource" vs “rails g scaffold” - When to Use Which?

Tushar Adhao
3 min readDec 16, 2024

Rails makes setting up models, controllers, and views quick and easy with its built-in generators. Two of the most commonly used are rails g resource and rails g scaffold. While they seem similar, each serves a different purpose depending on how much control and customization you need. In this post, we’ll explore the differences and help you decide when to use each one.

Understanding Rails Generators

Rails generators help you quickly build out the structure of your application. They can create models, controllers, views, migrations, routes, and even tests. This automation saves time, so you can focus on logic instead of boilerplate code.

But which generator should you choose for a new resource in your application? Let’s dive in.

“rails g resource”: Build the Basics and Customize

The rails g resource command is a minimalistic generator. It creates the essential files for a resource but gives you the flexibility to build out the controller actions and views yourself. This command is perfect for developers who want control over each aspect of their code and may only need a model and some core structure.

Example Usage:

rails g resource Article title:string body:text

This command creates:
- Model: app/models/article.rb
- Migration: db/migrate/2024….create_articles.rb to create an articles table with title and body columns.
- Controller: app/controllers/articles_controller.rb (empty, no actions by default)
- Routes: Adds resources :articles to config/routes.rb

What’s Missing? rails g resource does not add:
- View Templates: You’ll need to create views manually.
- Controller Actions: CRUD actions like index, show, or new are not included, giving you the freedom to define exactly what you need.

“rails g scaffold”: Full CRUD Interface in One Go

The rails g scaffold command is more comprehensive. It generates everything that rails g resource does, but it also includes a complete set of controller actions and views for a standard CRUD interface. If you want a quick, ready-to-go interface to create, read, update, and delete records, rails g scaffold is your friend.

Example Usage:

rails g scaffold Article title:string body:text

This command creates:
- Model: app/models/article.rb
- Migration: db/migrate/2024….create_articles.rb to create an articles table with title and body columns.
- Controller: app/controllers/articles_controller.rb with actions like index, show, new, edit, create, update, and destroy.
- Routes: Adds resources :articles to config/routes.rb.
- Views: Generates all the necessary views (index.html.erb, show.html.erb, new.html.erb, edit.html.erb) for the CRUD actions.
- Basic Test Suite: Generates basic test files for the controller and model.

With these files, you have a fully functioning CRUD (Create, Read, Update, Delete) interface for Article.

When to Choose “rails g resource” or “rails g scaffold”

  • Choose rails g resource if:
    — You need only the model, migration, and basic controller setup.
    — You prefer to manually add actions and views.
    — You have custom actions or a unique resource structure.
  • Choose rails g scaffold if:
    — You want a complete CRUD interface fast.
    — You’re creating an admin interface, prototype, or MVP.
    — You want a starting point that covers the basics and is easy to customize further.

Advantages of Each Approach

  • rails g resource is flexible and lets you define only what’s necessary, keeping your project lightweight and maintainable.
  • rails g scaffold saves time with a full CRUD setup, allowing you to rapidly prototype or test resource functionality.

Conclusion

Both rails g resource and rails g scaffold offer great options for generating resources in Rails. While rails g resource is more granular and customizable, rails g scaffold provides a fast, complete CRUD solution. By understanding the differences, you can choose the generator that best fits your project needs, saving time and ensuring the right balance between control and automation.

--

--

Tushar Adhao
Tushar Adhao

Written by Tushar Adhao

Software artist spreading nuggets of coding gold and sometimes philosophy too.

No responses yet