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 7
Page 7 of 8

Finally, I tweak the code to get a clickable headline. Instead of manually coding the <a href= > HTML link, I can use the Ruby on Rails link_to function followed by the text I want to be clickable, and then the link itself:

<p>The last story added to my database of favorite Ruby on Rails stories is <%= link_to @last_story.headline,@theurl%>.</p>

Final shot


With logic in one file and display code in another, my little RoR app pulls out the most recent entry to my database and generates a clickable hyperlink.

And so, a Ruby on Rails Model-View-Controller application is born.

Cool stuff
There's so much more to Ruby on Rails, though:

  • Autogenerated testing files designed to make it easy to code tests as you're creating the application itself, instead of tacking it on as an afterthought (or in my case of coding personal apps, blowing it off entirely).
  • Numerous "helper" modules making it easy to display things like forms, dates, links and more.
  • Built-in support for AJAX, so you can create interactive interfaces (such as drag and drop) with a minimum of code.

Generating my single page with a few database calls took a fair amount of ramping up and an awful lot of files. I later discovered that deploying a Rails application to a production environment takes a few more steps than simply uploading files. Nevertheless, I've been convinced that the Ruby on Rails framework would make building and maintaining more complicated applications faster and easier in the long run. It will be less work to access and manipulate information in my database, and to update my code.

As a final example, creating a Web form with the built-in Rails form_for helper is surprisingly easy. Adding this code to a view:

<%form_for :day do |f| %>
Enter the date:
<%= f.date_select :thedate %>
<%= submit_tag %>

<% end %>

creates a form that includes drop-down lists for year, month and date (automatically selected to today's date), as well as a Submit button. The HTML behind the form also generates names for each field that are associated with a specific class, so whatever is submitted gets added to the right part of your database. See Ease Web form creation with Ruby on Rails for more details.


Object-oriented programming explained ... in English!
Jargon such as "The instantiation of a new object that's based on an existing class is accomplished by sending that class the new message" is what makes me want to run screaming into the night when trying to learn object-oriented programming (OOP). Fortunately, most of Lenz's explanations of OOP in Build Your Own Ruby On Rails Web Applications are a lot more readable.

It turns out that many OOP concepts are pretty simple. Basically, you start off with some kind of group. People. Cars. Dogs. Strings (here in the programming sense of "a group of letters, numbers and other characters," and not "twine"). Each group has individual members. You and I are each "people." My 1994 Saturn is "car." Lucky the poodle is a "dog." And so on. Easy enough.

All members of the group have certain characteristics just because they're part of the group. Some are default values: All members of the class "Car" have four wheels, all members of "Dog" have four legs (we're excluding accidents here in both cases). Some values are different for each individual member. My Social Security number is different from yours, although we both have one (assuming we're both members of the "AmericanCitizen" group). OK, that's not so complicated.

Along with having characteristics, individual members of our groups can also do things. Lenz uses as an example "open_trunk" or "start_engine" actions for members of the "Car" group. There could be a "bark" function for dogs or a "transform to all uppercase letters" function for strings. If you're still with me here, that's pretty much the foundation of OOP.

Now, we just need to translate these concepts into OOP jargon.

The group we started out with (people, cars, strings, etc.) is called a class in OOP-speak. Names of classes are usually singular, capitalized nouns in Rails. So our classes would be named People, Car, Dog and String.

The individual items in each group are objects.

The characteristics of objects in the group (number of wheels, Social Security number) are their attributes.

The things that our objects can do -- start the engine, bark, be transformed to uppercase -- are called methods (or sometimes in Rails, actions).

Finally, creating a new object, one item of the type defined by the class -- what we'd call birth when talking about people or dogs in the real world -- is known in the OOP world as instantiation. If I've got a class called Story in my Ruby on Rails project, the class has a lot of definitions of what any story object should be like and be able to do. If I want to create an actual story item represented by the variable mystory, I could simply use the command mystory = Story.new, and my story object would be ready for action.

1 2 3 4 5 6 7 8 Page 7
Page 7 of 8
7 inconvenient truths about the hybrid work trend
Shop Tech Products at Amazon