Hands on with Ruby on Rails

Follow along as our editor learns this hot Web dev framework

1 2 3 4 5 6 7 8 Page 5
Page 5 of 8

I return to my Rails command shell within the myfavorites directory, and run the command

rake db:migrate

to execute my migration file. I take a peek via phpMyAdmin and, sure enough, a stories table has been created with appropriate rows: contentid, headline and summary, as well as an autocreated primary key of id.

Now, each new story object I use in my application can easily be saved as an entry in my stories table.

Next, I generate a Story controller and an associated view named index by running this command in a Ruby console:

ruby script/generate controller Story index

By running the script/generate controller with the options Story and index, Rails generates a Story controller associated with my Story model and an index file that will handle the presentation HTML to display information to users.

First look at the results
Now the bare bones of all three parts of the MVC are in place. I can see what the default looks like by starting the WEBrick server (included with Ruby on Rails) with the command

ruby script/server

and going to http://localhost:3000/story in a browser.

If everything was created properly, I should see this default text in my browser:

clear.gif

Success

clear.gif

Success! This message shows that my controller and view files are communicating properly.


This, Lenz explains, shows the connection between controller and view is working, because the index view can display information from the controller file.

The joys of scaffolding
For fun, as Lenz suggests, I next go into the story_controller.rb file (in the app/controllers directory) and edit it so it says:

class StoryController < ApplicationController
scaffold :story
end


instead of the autogenerated

class StoryController < ApplicationController

def index
end
end


 
Scaffolding 2

clear.gif

With one line of code, I get a basic administrative interface for entering data.

Now I'm impressed. With that one line of code, I get a quick and dirty interface to my database where I can add data.

Now I enter a couple of stories from Computerworld.com into the myfavorites database:

Contentid: 106458
Headline: QuickStudy: Ruby on Rails
Summary: Ruby on Rails is a software development environment with the overall aim of making programming both more fun and more productive.

Contentid: 9002857
Headline: Ruby on Rails hands on: What's so hot about Rails?
Summary: Why all the buzz around this development framework? Because creating database-driven Web apps can be faster and simpler. Find out more in this excerpt from O'Reilly Media's Ruby on Rails: Up and Running.

Contentid: 9018460
Headline: Rails for Java Developers
Summary: Using the Rails framework, Ruby is giving Java a run for its money.

 
Scaffolding 2

clear.gif

The scaffolding interface allows you to view existing database entries as well as add new ones.

All these entries show up in the scaffolding, along with the option to add new stories.

Then, to get back to where I can see the index.rhtml view in my browser, I replace scaffold :story with

class StoryController <
  ApplicationController

def index
end
end


which was there before. I need to make sure there are now two "end" lines in the file, one ending the class definition and one ending the index method. (I quickly find that leaving off "end" commands is among my most common Ruby errors.)

1 2 3 4 5 6 7 8 Page 5
Page 5 of 8
  
Shop Tech Products at Amazon