Ruby on Rails Scaffolding - Ruby on Rails

When is Scaffolding method used?

Scaffolding method is the recommended method to use while you're developing Rails applications, especially those which are mainly providing you with a simple interface to data in a database.

What are the benefits of Scaffolding?

Some of the benefits of scaffolding are:
  • It is easy to get code in front of your users for feedback.

  • Scaffolding assures faster success ad you motivated by faster success.

  • You can learn how Rails works by looking at the generated code.

  • You can use scaffolding as a foundation to jump start your development.


Scaffolding Example

Let's understand scaffolding by creating a database called cookbook and a table called recipes.

How to create an Empty Rails Web Application

Open a command window and navigate to where you want to create this cookbook web application. Use the following command to create a complete directory structure.

How to Set up the Database

Here is the way to create a database −
Edit the configuration file cookbookconfigdatabase.yml and change the database name to cookbook to instruct Rails how to find the database. Leave the password empty. This is how it looks once you finish−
Rails lets you run in the development mode, test mode, or production mode, using different databases. This application uses the same database for each.

How to generate Scaffold Code manually?

With the scaffold action, Rails generates all the code it needs dynamically. By running scaffold as a script, we can get all the code written to disk, where we can investigate it and then start tailoring it to our requirements.
So now, let's start once again to generate Scaffold code manually by using the scaffold helper script −
It generates auto-files as shown below −
Auto files of Scaffold

What is the code behind the Controller?

The code behind the controller is generated by the scaffold generator. If you open app/controllers/recipes_controller.rb, then you will find something as follows −
When the user of a Rails application selects an action, e.g. "Show" - the controller will execute any code in the appropriate section - "def show" - and then by default will render a template of the same name - "show.html.erb". This default behavior can be overwritten.
The controller uses ActiveRecord methods such as find, find_all, new, save, update_attributes, and destroy to move data to and from the database tables. Note that you do not have to write any SQL statements, rails will take care of it automatically.
This single line of code will bring the database table to life. It will provide with a simple interface to your data, and ways of −
  • Creating new entries
  • Editing current entries
  • Viewing current entries
  • Destroying current entries
When creating or editing an entry, scaffold will do all the hard work like form generation and handling for you, and will even provide clever form generation, supporting the following types of inputs −
  • Simple text strings
  • Text areas (or large blocks of text)
  • Date selectors
  • Date-time selectors
You can use Rails Migrations to create and maintain tables.
Next, go to the cookbook directory and run the Web Server using the following command −
At this point, open a browser and navigate to http://127.0.0.1:3000/recipe/new. The following screen to create new entries in the recipes table will appear.
Creating new recipe
Once you press the Create button to create a new recipe, your record is added into the recipes table and it shows the following result −
Create recipe
Play around with the edit, show, and destroy the records options that you see.
You can also list down all the recipes available in the recipes table using the URL http://127.0.0.1:3000/recipe/list.

How to enhance the Model

Rails gives you a lot of error handling for free. To understand this, add some validation rules to the empty recipe model −
Modify app/models/recipe.rb as follows and then test your application −
These entries will give automatic checking.
  • validates_length_of − the field is not blank and not too long.

  • validates_uniqueness_of − duplicate values are trapped. Instead of the default Rails error message, we have given a custom message here.


What is the alternative way to create Scaffolding?

Create an application as shown above and The Generated Scaffold Code
Above code generates the auto files with data base by using with sqlite3 with tittle and instruction column as shown below an image.
Alternative ways of scaffolding
we need to migrate the data base by using below syntax.
Finally run the application by using the following command line −

It will generate the result as shown above output images.

The Views

All the views and corresponding all the controller methods are created by scaffold command and they are available in the app/views/recipes directory.

Scaffolding creates methods to list, show, delete and create data etc automatically.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Ruby on Rails Topics