1. Scaffolding method
Scaffolding method build a fundation to go further while
developing a project. It is useful mainly when
developing Rails applications dealing with a
database.
2. Example:
The sulfur compounds contain sulfur atom. They include
Hydrogen sulfide, Mercaptan (or Thiol), Sulfide, Disulfide,
Thiophene, Benzothiophene, and many others.
We will set for them a database sulfurs
and a table properties.
The steps
- Create the Rails Web Application:
C:\ruby> rails sulfurs
Create its related database:
mysql> create database sulfurs;
Query OK, 1 row affected (0.05 sec)
(grant and FLUSH PRIVILEGES)
Configure the related database:
Go to
c:\ruby\sulfurs\config\database.yml
Creating the related table "properties":
mysql> USE sulfurs;
Database changed
mysql> CREATE TABLE properties(
id INT(15) NOT NULL AUTO_INCREMENT,
...
);
Query OK, 0 rows affected (0.13 sec)
Creating Model:
Create a "Sulfur" model class. (Sulfur is capitalized
and used in its singular form according to Rails rules.
The model holds data from the properties table in the
database.
C:\ruby\sulfurs> ruby script\generate model Sulfur
That creates the empty file app/models/sulfur.rb
that will contain definitions for the Sulfur class.
Creating Controller:
we will create sulfur controller (the same name
as for model) with actions to manipulate the sulfurs
in the database via the standard CRUD operations: create,
read, update, and delete.
C:\ruby\sulfurs > ruby script\generate controller sulfur
That creates app/controllers/sulfur_controller.rb file
that will contain definitions for the sulfurController class.
Generated Scaffold Code:
Issue the command line:
C:\ruby\sulfurs>ruby script/generate scaffold sulfur
That outputs:
exists app/models/
exists app/controllers/
exists app/helpers/
create app/views/sulfurs
exists app/views/layouts/
exists test/functional/
exists test/unit/
create app/views/sulfurs/index.html.erb
create app/views/sulfurs/show.html.erb
create app/views/sulfurs/new.html.erb
create app/views/sulfurs/edit.html.erb
create app/views/layouts/sulfurs.html.erb
create public/stylesheets/scaffold.css
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
identical app/models/sulfur.rb
identical test/unit/sulfur_test.rb
identical test/fixtures/sulfurs.yml
exists db/migrate
C:\ruby\sulfurs>
The scaffold generator has created all the above file.
with some few lines of code
All the views methods (list, show, delete and create) are created
by scaffold command and they are available in app/views/sulfurs
created directory; that have to be copied and pasted to
app/views/sulfur also created .
3. Remark:
This scaffold utility set new entries and provides with
the definitions edit, view and delete to edit, view and destroy
the current entries. In other word, It does the job; obviously,
this is not the best way to follow if we are trying to learn.
|