comfort zones are for sleeping

Showing posts with label Getting Started. Show all posts
Showing posts with label Getting Started. Show all posts

Monday, September 3, 2007

Hello, Rails! Part Deux: Season of the Witch

OK, so dynamic-izing the Hello, Rails! app turned out to be a heck of a lot easier than I had assumed it would be. Well, not really - I mean I thought it would be pretty easy, after all this is just a Hello, World app, and RoR is advertised as high-productivity. But it was stinkin' easy, man.

Still in the Hiney book on chapter 4, I messed around with some code they said I should plug in here and there. I wanted to display the current time, plus link some pages together, no big deal. Here's what the code ended up looking like:


say_controller.rb

class SayController < ApplicationController

def hello

@time = Time.now

end

def goodbye

end

end

hello.rhtml

<html>
<head>
<title>Hello, Rails!</title>
</head>
<body>
<h1>Hello from Rails!</h1>
<ul>
<li>Addition: <%= 1+2 %> </li>
<li>Concatenation: <%= "Chew" + "bacca" %> </li>
<li>Time in one hour: <%= 1.hour.from_now %> </li>
</ul>
<% 3.times do %>Ho!<br/><% end %>
Merry Christmas!
<p>It is now <%= @time %></p>
<p>Time to say <%= link_to "GoodBye!!", :action => "goodbye" %></p>
</body>
</html>



goodbye.rhtml

<
html>
<
head>
<
title>See you later!</title>
</
head>
<
body>
<
h1>Goodbye!!</h1>
<
p>It was nice having you here.</p>
<
p>Say <%= link_to "Hello!!", :action => "hello" %> again.</p>
</
body>
</
html
>

Now I've got two pages that link to each other through the controller using the link_to helper method. And the hello.rhtml file sits up and does a few interesting little tricks itself. I'll leave it to you to figure out exactly what each line does. Easy, breezy, beautiful, Cover Girl.

What's next?
I'm glad you asked. I think I'm going to put down the Hiney book for a few days and focus on learning a bit about Ruby itself. I got a copy of
the Pickaxe Book, but I'm planning to go through Why's Poignant Guide to Ruby first. There's a dude here in town named Bret Pettichord, and when it comes to Ruby he's a major dude. In fact, he was recently awarded a Certificate of Awesomeness by the Awesomeness Awareness Council. Anyway, Bret sez he picks up Why's Poignant Guide and reads through it from time to time. So I figure if he gets something out of it, a guy like me should get a lot out of it.



Saturday, September 1, 2007

A good RoR editor

Found a good editor for RoR. It's called RadRails - they mention it on the RoR download page, down towards the bottom. Everything is falling into place...

Now, if I could only find something that would make blogging code a bit easier.

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.

Sunday, August 26, 2007

Get Started, Start a Fire

I'm a .Net programmer. Some programmers say that with embarrassment or downright shame these days, but not me. Now, I'm sure good ol' uncle Bill and crew have engaged in some rather nefarious business practices over the years, but what major U.S. corporation doesn't have a few skeletons in the closet? Hey, programming the Microsoft way has fed my family for years. Frankly, I'm far too self-centered to spend my time developing political opinions about gay skeletons. So I have neither a war to wage nor an axe to grind against Microsoft. I'm fairly satisfied with the .Net programming opportunities available to me in the marketplace (that's in Austin, TX, by the way). I'm comfortable creating applications based on the .Net framework, and I will continue doing that professionally.

So why bother with Ruby on Rails? The simplest answer is: I want to be a better programmer. Like I said, I'm comfortable with .Net. But I readily admit that Microsoft's marketing plan does not necessarily lead programmers to create software using the highest degree of craftsmanship. It's time to be uncomfortable again. Even if I never use RoR professionally, I want to broaden my understanding of my chosen profession.

I will use every resource available to me to further my education and progress. This includes easily available stuff like http://www.rubyonrails.org/, RoR books, tons of online samples and tutorials, etc., as well as local resources like the Austin on Rails developer community. Hopefully, I'll be successful enough with my experiments to post some of them online, so that the world might gratefully acknowledge my genius by mailing me hundreds of cards and letters containing their good wishes, offers of moral support and prayers for my progress along this pilgrimage, and small honorariums (cashier's checks only please, no personal checks).

So here we go.