Getting Started with Ruby on Rails - Rails in the Real World
(Page 4 of 4 )
Since this is the first discussion of RoR, I won't be presenting a heavy duty example. However, the example to be presented will show the ease of development in RoR. So the example will be based on the Customer Order example I used in the section about MVC. The example will do the following:
1. Provide a form for entering the order details.
2. Provide a list view of all the orders placed.
3. Provide a form for modifying the data.
4. Provide a delete option for a specified order.
Before beginning the development, a database and table must be created because I will be using MySQL. There are certain conventions to be followed for the naming of the table and its fields:
- The table name should be plural, i.e. in my case the table name would be Orders.
- The primary key should be of the type INT (for MySQL tables) and its auto increment attribute should be set to true.
- Keeping the column names in lower case is recommended.
Keeping these points in mind, here is the table definition (the name of the table is demo_development):
CREATE TABLE `orders` (
`id` INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`product_name` VARCHAR( 20 ) NOT NULL ,
`cost` VARCHAR( 10 ) NOT NULL
) TYPE = innodb;
Now let's create the application. The name of the application is demo. So the command would be:
rails demo
Then change the config directory of the demo directory; the database.yml has to be changed in this way:
development:
adapter: mysql
database: demo_development
username: raj
password:
host: localhost
Next, to create a full-fledged database driven application, give the following command at the root directory of the application i.e. “demo”:
ruby script/generate scaffold order admin
Here the scaffold argument generates boilerplate code for all three components, Model, View and Controller for the given table (Orders). The name of the controller is optional, though I have given it. This would create the ORM of the table name, the controller of the given name and the RHTMLs for the add, modify, delete and view functions. The name of the table has to be given in singular, otherwise the application would throw an exception when launched. To check what has been created, start the server, and give the following URL at the address bar:
http://localhost:3001/demo/admin
That’s it. RoR creates all the boilerplate code required for CRUD operations. But there are various aspects that have to be understood before an actual application can be built. Starting with the next part, I would be discussing the Model, View and Controller components provided by Ruby-on-Rails.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |