• home
  • forum
  • my
  • kt
  • download
  • Learn how to use Rails Scaffolding

    Author: 2007-08-25 15:40:08 From:

    While you're developing Rails applications, especially those which are mainly providing you with a simple interface to data in a database, it can often be useful to use the scaffold method.

    Scaffolding provides more than cheap demo thrills. Here are some benefits:

    • You can quickly get code in front of your users for feedback.
    • You are motivated by faster success.
    • You can learn how Rails works by looking at generated code.
    • You can use the scaffolding as a foundation to jumpstarts your development.

    Scaffolding Example:

    To understand scaffolding lets create a database called cookbook and a table called recipes:

    Creating an Empty Rails Web Application:

    Open a command window and navigate to where you want to create this cookbook web application. I used c:\ruby. So run the following command to create complete directory structure.

    C:\ruby> rails cookbook
    

    Setting up the Database:

    Here is the way to create database:

    mysql> create database cookbook;
    Query OK, 1 row affected (0.01 sec)
    
    mysql> grant all privileges on cookbook.*
    to 'root'@'localhost' identified by 'password';
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> FLUSH PRIVILEGES;
    Query OK, 0 rows affected (0.00 sec)
    

    To tell Rails how to find the database, edit the configuration file c:\ruby\cookbook\config\database.yml and change the database name to cookbook. Leave the password empty. When you finish, it should look something like

    development:
      adapter: mysql
      database: cookbook
      username: root
      password: [password]
      host: localhost
    test:
      adapter: mysql
      database: cookbook
      username: root
      password: [password]
      host: localhost
    production:
      adapter: mysql
      database: cookbook
      username: root
      password: [password]
      host: localhost
    

    Rails lets you run in development mode, test mode, or production mode, using different databases. This application uses the same database for each.

    Creating the database tables:

    We will use following table for our practical purpose. So create recipes table from sql prompt as follows:

    mysql> USE cookbook;
    Changed database
    mysql> CREATE TABLE recipes (
        -> id INT(11) NOT NULL AUTO_INCREMENT,
        -> title VARCHAR(40),
        -> instructions VARCHAR(255),
        -> PRIMARY KEY (id));
    Query OK, 0 rows affected (0.06 Sec)
    

    NOTE: If you wish you can use Rails Migrations to create and maintain tables.

    Creating Model:

    First, create a Recipe model class that will hold data from the recipes table in the database. Use the following command inside cookbook directory.

    C:\ruby\cookbook > ruby script\generate model Recipe
    

    Notice that you are capitalizing Recipe and using the singular form. This is a Rails paradigm that you should follow each time you create a model.

    This will create a file named app/models/recipe.rb containing a skeleton definition for the Recipe class.

    Creating Controller:

    Now we to create a recipe controller with actions to manipulate the recipes in the database via the standard CRUD operations: create, read, update, and delete.

    C:\ruby\cookbook > ruby script\generate controller Recipe
    

    Notice that you are capitalizing Recipe and using the singular form. This is a Rails paradigm that you should follow each time you create a controller.

    This will create a file named app/controllers/recipe_controller.rb containing a skeleton definition for the RecipeController class. Edit this file and add the line scaffold:recipe as shown

    class RecipeController < ApplicationController
       scaffold:recipe
    end
    

    This single line of code will bring the database table to life. This 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 of form generation and handling for you, and will even provide clever form generation, supporting the following types of inputs:

    • Simple text strings
    • Textareas (or large blocks of text)
    • Date selectors
    • Datetime selectors

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Database Related (2)
      Getting Started (7)
      Helpers (4)
      Image Manipulation (2)
      Security (4)

    New

    Hot