Hear Me Ro(a)R
Ruby on Rails (RoR) Rocks! It is off the chain, off the hook, and off the tracks!! Like Struts, RoR is a Model 2 framework used to build web applications. With RoR you define actions in controller classes and delegate the presentation to rthml files. RoR favors convention over configuration, which means no XML configuration files such as struts-config.xml. Ruby on Rails is getting a lot of press and hype these days. There already is a book on RoR and more are on the way while Ruby on Rails is just at version 0.12.
The power of Ruby of Rails, in mind, is the tight integration with the O/R mapping tool used (ActiveRecord). For example, once you configure Rails with your database via the database.yml file, the only configuration file I was able to find, a model class for a Product consists of the following code:
class Product < ActiveRecord::Base end
In the database you could use the following SQL to define the products table.
create table products ( id int not null auto_increment, title varchar(50) not null, description varchar(50) not null );
Just with the Product model and the products table defined above, you can have Ruby code in you action such as:
def show
@product = Product.new
@product.title = "This is my title"
@product.description = "This is my description"
if @product.save
flash[:notice] = "Product saved!"
else
flash[:notice] = &qout;Product was not saved!&qout;
end
end
If you thought that the code for the model was too much to type, you can use the Rails scripts to generate it!! You can generate controllers, actions, and models using Rails scripts.
On the view side you have the same scriptlet tags as in JSP, such as in the show.rhtml page associated with the show action above, you can have the following code to print out a product’s title:
<B><%= @product.title %></B>
How easy is that? I am starting to play with Ruby on Rails so I am sure to be writing more on it.