Geeks With Blogs
.net alternatives by Michel Grootjans

I'm taking this step. This technology has been on my radar for quite a while. Now I'm taking the plunge.

I've bought this book that looks really promising on the subject. RailsSpace: Building a Social Networking Website with Ruby on Rails. I'm halfway chapter 6 right now, and I'M LOVING IT. I have decided to make the most out of this experience, so I'm going to build this application along with the book. I've set up my little SVN on Google where I will keep my evolution up to date.

Chapter 1: Introduction.

This one's easy. Just a narrative introduction.

Chapter 2: Getting started.

This chapter explains how to install Ruby, RubyGems and Rails. I also installed a mySql database since RoR seems to work well with it. It claims to be databse agnostic (shouldn't that be database polytheistic ?), so SqlServer shouldn't be a problem, but I don't want to go too far off the beaten path of the book.
All the major platforms are supported in the book, but the examples are Mac-based. I'm WinXP based, and up until now this hasn't bothered me a bit.

Then it moves to the actual creation of a project. This is the first step that made me feel all warm inside. Go to the console, navigate to whatever directory (to me that's d:\\projects) and type 'rails the_name_of_the_project_you_want'. That's it! You now have your whole project structure set up. Let me run you trough it quickly:

  • app
  • config
  • db
  • lib
  • log
  • public
  • script
  • test
  • tmp
  • vendor

Next I just start the web application by typing 'ruby script/server'. Bam, I've got a web site running on http://localhost:3000/ (don't click on this link... you might be disappointed). Of course this is not much, but it's a start.

Next I generate my first Controller with a few Views. I type 'ruby script/generate controller Site index about help'. This creates a controller named Site_controller and three views: index, about and help. That's it, we've basically built three web pages for our site. They're all using a master layout, similar to a master page in asp.net.

A nice highlight on what you can do in a view:

link_to "Home", :controller => :site, :action => :index

As a C# developer, the syntax looks a little alien to me, but I've had worse ;-) This inserts a link in a view that points to the action (==method) 'index' of the 'site_controller' class. You can exchange 'link_to' with 'link_to_unless_current' which will do exactly what it says.

Chapter 3: Modeling users

In RoR, models seem to be what we understand under Domain Objects in .net. Before I start modeling, I have to set up a database. TDD, BDD, DDD and other *DD aficionado's will probably be frowning right now, but bear with me. Magic's up ahead! In the config directory, theres a file called database.yml. This file holds the connection definition to all the databases I will be using during my development lifecycle: development, test, production. I just need to create two of these three databases: dev and test.

Next, I type 'ruby script/generate model User'. This generates a bunch of files. One particularly caught my attention: db/migrate/20080816134931_create_users.rb

After adding the fileds I need, the file looks like this:

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|

      t.column :screen_name, :string
      t.column :email,       :string
      t.column :password,    :string
    end
  end

  def self.down
    drop_table :users
  end
end

Do you see the magic? This file describes declaratively what needs to be done to the database to move to version 1 of the app. No SQL statements, just a declaration of the intent. At this time, the actual database is still empty. Let's change that: 'rake db:migrate'. The development database now contains the right table with the necessary records. 'rake db:migrate VERSION=0' will reverse the database to version 0. Further up on the road, you will be able to type 'rake db:migrate VERSION=0080816134931' to revert to the db version like it is now. Imagine the ease of deployment and rollback of deployments.

The road ahead

There's plenty of magic coming up. Chapter 3 isn't completely covered here. I'll be posting regular updates on this blog to keep you informed on my journey in this completely new world.

Posted on Friday, August 29, 2008 12:51 AM Ruby on Rails | Back to top


Comments on this post: Ever tried Ruby on Rails?

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Michel Grootjans | Powered by: GeeksWithBlogs.net