comfort zones are for sleeping

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.



No comments: