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 is the command that generates a set of perfectly working nested resource for Rails 4.2 and 5.
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.
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.
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.
Go to New Post
Click on Create.
Click on Edit.
Click on Update.
DownloadDownload this example