Ruby on Rails Scaffolding

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.

Features

  • 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

Syntax

To install nested scaffold, use the following command.

gem 'nested_scaffold'  

Creating a Resource

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

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

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

Following files will be created with scaffolding.

FilePurpose
db/migrate/20100207214725_create_posts.rbCreates the post table in your database
app/models/post.rbThe Post model
test/unit/post_test.rbUnit testing harness for posts model
test/fixtures/posts.ymlSample posts for use in testing
config/routes.rbEdited to include routing information for posts
app/controllers/posts_controller.rbThe posts controller
app/views/posts/index.html.erbA view to display index of all posts
app/views/posts/edit.html.erbA view to edit an existing post
app/views/posts/show.html.erbA view to display a single post
app/views/posts/new.html.erbA view to create a new post
app/views/posts/_form.html.erbA partial to control the overall look and feel of the form used in edit and new views
test/functional/post_controller_test.rbFunctional testing harness for posts controller
app/helpers/posts_helper.rbHelper functions to be used from the post views
test/unit/helpers/posts_helper_test.rbUnit testing harness for the posts helper
app/assets/javascripts/posts.js.coffeeCoffee script for post controller
app/assets/stylesheets/posts.css.scssCascading style sheet for post controller
app/assets/stylesheets/scaffolds.css.scssCascading 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

rails new example  

Step 2 In the example application, create MVC components.

cd example  

rails generate scaffold post title:string body:text  

rails generate scaffold comment post_id:integer body:text

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

Step 3 Create database tables comments and post_id.

rake db:migrate  

Step 4 Use rake command to run migrations.

rake routes  

Step 5 Start the web server

rails server  

Output:

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

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *