comfort zones are for sleeping

Saturday, September 1, 2007

Hello, Rails

Well, it's here. The time has finally come for an actual RoR application. And since this is my first RoR application, I will follow the KISS principle: Gene Simmons is the coolest bass player ever. Granted, that's not really saying much. Bass players aren't exactly known for being cool, at least not the white guys anyway (have you seen Geddy Lee?). But I suppose being the coolest out of that crowd makes you worthy of some attention. And Gene always says, "Keep it simple, or I'll kick your Hiney!!" So I'm gonna dance the dance that's been passed down from father to son for generations of programmers: the time-honored Hello, World! app.

When we last left our intrepid hero, he was pooped. He had just finished successfully installing Ruby, RubyGems, Rails, WEBrick, and MySql, and he had pulled the sample app that ships with RoR up in a browser. There was the press conference, the autograph hounds, the paparazzi... It was quite a day. But now I have to write a real RoR app, and I still don't know anything about Ruby, or how to compile a Ruby program, how to navigate and manipulate the Rails framework, or anything! I don't even know what editor would work best for Ruby programming! Woe is me!

Never fear - Hiney's here! (Insert fanfare here) That's right, I'm back on the Hiney book (get your mind out of the gutter, Earl), chapter 4. Sez here Rails is an MVC framework. Let's not worry too much about what MVC is right now. Just know that in order to get Hello, Rails! up and running, we only need to worry about the V and the C, the View and the Controller. I want the app to say "Hello, Rails!" I need a view which will display stuff in a browser, and a controller whose job is to wake the view up and tell it what to do. RoR has a ton of stuff already built in to help us build each piece we need in a flash.

The Say Controller
So back in my command window, in the demo directory, I type this to generate a controller called Say:

c:\rubysandbox\demo<ruby script/generate controller Say

RoR thinks a minute, then spits out all this stuff:

exists app/controllers/
exists app/helpers/
create app/views/say
exists test/functional/
create app/controllers/say_controller.rb
create test/functional/say_controller_test.rb
create app/helpers/say_helper.rb

and then dumps me back out to a command prompt. Looks like it created some directories and files - whooptie-doo. Interesting filenames, though. There's a directory for views, another directory for controllers, the say controller file itself, another file apparently for unit testing the say controller, and something called say_helper.rb. Wonder what that is? Let's look at the controller - since I don't know what editor to really use, I'll just look at it in good ol' Notepad:

class SayController < ApplicationControllerend

I think Notepad is choking on some funky whitespace character in there. I think it's really supposed to look like this:

class SayController < ApplicationController
end

Ruby is a true object oriented programming language. This file contains a class called SayController that inherits all its behavior from another class called ApplicationController, kind of like the way I inherited my father's dainty hands and my mother's patchy facial hair. Thanks to modern medical science though, we don't have to accept nature's atrocities. Let's do some surgery on our class. We need to create an action, which the webserver will use respond to a url somebody types into the browser like this:

http://localhost:3000/demo/say/hello

Urls are a bit different in RoR apps then they are in .NET apps. In .NET apps, a url usually identifies a real physical resource residing somewhere on the webserver. In RoR, everthing in the url above up through and including
http://localhost:3000/ identifies the application we're trying to interact with. The say/ part identifies our say controller, and the hello part identifies the action we're about to add to the say controller. Here's what the SayController class looks like with the hello action added into it:

class SayController < ApplicationController
def hello
end
end


I added a method to the SayController class called hello. There's nothing in that method right now, but that's ok. Remember, SayController inherits a lot of behaviour from ApplicationController.

The hello View
A controller without a view doesn't do much. So let's create the hello.rhtml view and save it in the app/views/say directory:

<html>
<head>
<title>Hello, Rails!</title>
</head>
<body>
<h1>Hello from Rails!</h1>
</body>
</html>

Pretty simple stuff. But what do you want? It's just a Hello, World! app. Remember, notepad likes to hang ".txt" on the end of your filenames. So take a look in the app/views/say directory and make sure that the filename for the view is hello.rhtml.

Putting it all together
So here's what will happen: Somebody (probably me) will come along and type
http://localhost:3000/say/hello into a browser on this computer. Assuming WEBrick is running, RoR will know it needs to look at the demo application and run the hello action on the say controller. The say controller will tell RoR to look in the app/views/say directory for a view called hello.rhtml - notice how all the names of various things are matching up (the say controller and the say views directory, the hello action and the hello.rhtml view, etc). RoR expects files and directories to be in standard locations, and for action and view names to match. When that happens, life is easy. RoR looks in the view directory, grabs hello.rhtml, and spits it back out to the browser.

First, I have to make sure my webserver is running:

c:\rubysandbox\demo>ruby script/server

and then I'll browse to
http://localhost:3000/say/hello in Firefox. And whaddaya know, it works!! There it is, "Hello from Rails!" smiling right back at me in the browser!! Awesome.

Next time
So what did we do this time? We learned about controllers and views, we created a controller and a view using RoR standards, and we got a static page we created to come up in the browser. That's good enough for today. Next time, we'll add a bit to Hello, Rails! to make it a bit more dynamic. But not today. I gotta go buy some paint.

No comments: