Why I Love Ruby on Rails

That first post I made about Ruby on Rails was more of a brain fart of excitement than a proper explanation of why it’s so cool. I really wanted to explain in my own words why I’ve decided I’d rather be working with RoR than any other framework, or at least the one I’m used to—PHP.

First of all, RoR is more than just a language; it’s a framework. This means that developers are encouraged to write applications in a roughly standard way, and all rails apps have a very similar flow. For instance, when you first begin a new rails application, you use the “rails” command line tool:

$ rails myapp

And rails generates all your starting directory structure for you—where to hold the application files, test code, external libraries, and so forth. And the fun doesn’t stop there! Say I have to write the same CRUD code I’ve down a thousand times in PHP to let an admin manage, say, users.

$ ruby script/generate scaffold User

Blamo! I get a model class that represents a user in the database, and a controller class complete with generated templates that handle all the flow for a simple CRUD. I need only to extend from this.

Rails uses what they call the Model-View-Controller (MVC) model. An instance of a model represents a single record in the database. It knows all about itself: how to validate its attributes, what its relationships to the other models are (which gives you lots of really sweet functionality for FREE!), and whatever else you want to program in. The view is nothing more than an erb (embedded ruby) template that spits out values calculated in the controller, which in turn is the class that dispatches and handles actions (incoming requests). Find yourself putting too much code into the view? Move the code into the helper (every controller has one) and call your new helper method from the view.

Here’s just a short example of how you might fetch some information about a product and display it to the user:

The controller, app/controllers/store_controller.rb:

class StoreController < ApplicationController
  def product
    @product = Product.find(params[:id])
  end
end

The view, app/views/store/product.rhtml

<% @product.attributes.each do |field, value| %>
  <%= field %>: <%= value %><br />
<% end %>

No lingering sql or extra code of any kind. Couldn’t you get used to that?

Yeah, you can do all the same stuff in PHP, but your code will probably turn out pretty ugly. I’m just loving how elegantly Ruby on Rails solves these web development issues. Just the other day, I started experimenting with AJAX and script.aculo.us js libraries to achieve some quite impressive effects and interactivity with very little effort.

The Rails people like to call it a “disruptive technology.” I’m not sure to what extent Rails is a disruptive technology, but I’m pretty sure there is a place for it in the future as we continue to see its user base grow like wild!

And there are lots of good tutorials and references at documentation.rubyonrails.com.

Saturday, September 10th, 2005 Software

@djthread

My Flickr Photostream

A photo on Flickr
A photo on Flickr
A photo on Flickr
A photo on Flickr
A photo on Flickr
A photo on Flickr
 

Categories

My History