Javatpoint Logo
Javatpoint Logo

Ruby on Rails Scaffolding


Scaffolding is a quick way to produce some major pieces of an application. For auto generating a set of models, views and controllers for a new resource in a single operation, scaffolding is used.

Scaffolding is a technique supported by MVC frameworks in which programmers can specify how application database may be used. The framework or compiler uses it together with pre-defined code templates to generate the final code that the application can use to perform CRUD in database entries, effectively treating the templates as a "scaffold" on which to build a more powerful application.

Scaffolding occurs at two different phases of the program lifecycle, design time and run time. Design time scaffolding produces files of code that can later be modified by the programmer. Run time scaffolding produces code on the fly. It allows changes to the design of the templates to be immediately reflected throughout the application.

Scaffolding on Rails

Scaffolding was made popular by the Rails framework.

When line scaffold :model_name is added to a controller, Rails will automatically generate all the appropriate data interfaces at run time.

An external command can also be used to generate Ruby code for the scaffold in advance, which is rails generate scaffold model_name. The generated script will produce files of Ruby code that application can use to interact with database.

As of Rails 2.0, dynamic scaffolding is no longer supported.

Nested Scaffold

Nested scaffold is the command that generates a set of perfectly working nested resource for Rails 4.2 and 5.


  • Generates a nested child resource with a single command
  • Generates a beautifully working bunch of code
  • Automatically generates appropriate model associations for ActiveRecord
  • Haml ready


To install nested scaffold, use the following command.

Creating a Resource

To generate a scaffold for the post resource, enter the following command:

The scaffold generator will build several files in your application with some folders.

Following files will be created with scaffolding.

File Purpose
db/migrate/20100207214725_create_posts.rb Creates the post table in your database
app/models/post.rb The Post model
test/unit/post_test.rb Unit testing harness for posts model
test/fixtures/posts.yml Sample posts for use in testing
config/routes.rb Edited to include routing information for posts
app/controllers/posts_controller.rb The posts controller
app/views/posts/index.html.erb A view to display index of all posts
app/views/posts/edit.html.erb A view to edit an existing post
app/views/posts/show.html.erb A view to display a single post
app/views/posts/new.html.erb A view to create a new post
app/views/posts/_form.html.erb A partial to control the overall look and feel of the form used in edit and new views
test/functional/post_controller_test.rb Functional testing harness for posts controller
app/helpers/posts_helper.rb Helper functions to be used from the post views
test/unit/helpers/posts_helper_test.rb Unit testing harness for the posts helper
app/assets/javascripts/ Coffee script for post controller
app/assets/stylesheets/posts.css.scss Cascading style sheet for post controller
app/assets/stylesheets/scaffolds.css.scss Cascading style sheet to make scaffolded views look better

Many experienced developers avoid scaffolding, instead prefer to write all or most of their source code from scratch. Because its automatically generated code may not fit into your application.

Scaffold Example

Let us generate following example with scaffold.

Step 1 Create an application

Step 2 In the example application, create MVC components.

From the above code, first move to the application directory.

Step 3 Create database tables comments and post_id.

Step 4 Use rake command to run migrations.

Step 5 Start the web server


Run http://localhost:3000/posts in your browser.

Ruby On rails scaffolding 1

Go to New Post

Ruby On rails scaffolding 2

Click on Create.

Ruby On rails scaffolding 3

Click on Edit.

Ruby On rails scaffolding 4

Click on Update.

Ruby On rails scaffolding 5


Download this example

Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials

B.Tech / MCA