There’s so much more to Ruby on Rails:
— 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:
Enter the date:
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 4 wheels, all members of “Dog” have 4 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.
Ruby on Rails resources
Here are some ways to find out more about Ruby and the Rails framework.
On the Web
The Ruby on Rails project site is a good starting point. The screencasts can get you jazzed about how quickly you can do development work with RoR, although you will have to know what you’re doing. The site includes documentation, a wiki and links to more resources such as mailing lists. This is where you can download Ruby on Rails for your own system.
Rolling with Ruby on Rails Revisited by Bill Walton and Curt Hibbs is a tutorial designed to “show you how to develop a Web-based, database-driven application using Ruby on Rails.”
Instant Rails is a complete Ruby on Rails package for Windows that is so easy to use, you don’t even have to install it. “You simply drop it into the directory of your choice and run it,” the site explains. “It does not modify your system environment.”
Developer Nathan Brown posted 30 slides from a mini-workshop he did about Ruby on Rails that offer some useful background and links.
If you’re new to Ruby as well as Rails, you’ll need to refer to language documentation fairly frequently. Ruby-doc.org is one good place to start. The official Ruby language home page is also good.
Although I’ve done rudimentary programming in a number of different languages and think I’ve got basic concepts down by now, I still find it comforting to start off learning a new language/environment with something basic, so I feel grounded in the syntax by going over fundamentals. If you like to learn this way as well, I recommend Daniel Carrera’s Learning Ruby. Although somewhat out of date (it was written before Rails was released), it’s a good quick introduction to the Ruby language. Another free, more detailed, pre-Rails introduction to Ruby is Programming Ruby.
Ruby on Rails books
Build Your Own Ruby on Rails Web Applications by Patrick Lenz (SitePoint Pty.). One of the best options for people who are new to frameworks and OOP, this is mostly understandable even to tech-savvy nondevelopers. I wish Lenz explained more about why things are coded as they are and how to apply the given code in other situations. That is especially true of the testing examples. However, if you’re diligent, you can work out much of this for yourself. After coding along, try out the things he shows in alternative situations. This may take longer, but it can help you to fully understand the examples. You’ll need more than this book to be proficient in Rails, but this is an excellent starting point.
Rails Cookbook by Rob Orsini (O’Reilly Media Inc.). Some of the tasks here may seem a bit contrived, phrased in a way to show off the tool Orsini wants to explain at the time (“You want to embed comments in your source code and run a program to extract those comments into a presentable format”). That’s not to say the tasks aren’t things you’d want to do. But some of them are not the typical “how do you count days since an arbitrary date?” items that are familiar to Cookbook series readers. I expect this will be a useful reference book once I leave the safety net of slightly altering sample applications.
Ruby on Rails Up and Running by Bruce A. Tate and Curt Hibbs (O’Reilly Media Inc.). This is not for beginners and doesn’t pretend to be; the book is aimed at “experienced developers who are new to Rails and possibly to Ruby.” I can’t say whether a strong developer who’s done nothing but non-OOP would be able to follow along, but I suspect this would be most useful to those who already know a language like Java.
Rails for Java Developers by Stuart Halloway and Justin Gehtland (The Pragmatic Programmers LLC). Java developers don’t need thousands of pages on basic concepts, so this book picks up where they can start putting it to work right away. The first database-driven application is up and running by page four. Topics covered: Programming Ruby, MVC applications, unit and functional testing, configuration and security.
Ajax on Rails by Scott Raymond (O’Reilly Media Inc.). This is specifically about using the Rails framework for creating cool Web interfaces.
I haven’t seen these yet, but they’re frequently cited as important references: Agile Web Development with Rails by Dave Thomas, David Heinemeier Hansson [creator of the initial Ruby on Rails framework], Leon Breedt and Mike Clark (Pragmatic programmers) and Rails Recipes by Chad Fowlser (The Pragmatic Programmers LLC).
Ruby Cookbook by Lucas Carlson & Leonard Richardson (O’Reilly Media Inc.). I’m a big fan of the O’Reilly cookbook series. While this one isn’t specifically about Ruby on Rails, it offers a slew of code to help you understand how to implement scores of tasks — things you might know how to do in other languages, but can’t do yet in Ruby. Beyond understanding the Rails framework, some comfort level with Ruby syntax is important for anyone serious about trying RoR. There is a substantial chapter on Ruby on Rails. I doubt I would have understood the introduction if I were coming in cold, but the actual recipes, starting with a simple application to show system status, can help cement understanding after working through Lenz’s book (see the first item above).
Learning Ruby by Michael Fitzgerald (O’Reilly Media Inc.). This is a good basic introduction to Ruby, one of the better books at balancing readable explanation with a reference you can use to find, “what’s the syntax for coding that again?” Be advised, however, that although the book’s cover touts Ruby as “the language that powers Rails,” the actual section on Rails is rather sparse.
Ruby by Example by Kevin C. Baird (No Starch Press). Just out this month, this book is mostly about Ruby but has a couple of chapters on Rails, including one that steps through a photo database application. This doesn’t pretend to give anything more than a brief taste of the Rails framework; it’s more about some interesting Ruby scripts and how they work.
The Ruby Way (2nd edition) by Hal Fulton (Addison-Wesley/Pearson Education). The second edition was updated to include things like Ruby on Rails (although surprisingly, there are only five or so pages specifically about RoR among its 750+). When starting the book, I couldn’t help wondering whether it was about learning a programming language or joining a cult; Fulton is definitely a devotee. “When I look at Ruby, I perceive a balance between different design goals, a complex interaction reminiscent of the n-body problem in physics,” he writes in the introduction. “It is perhaps this interaction itself, the harmony, that embodies Ruby’s philosophy rather than just the individual parts.” And here I thought I was just trying to find a faster, more intuitive way to get database-driven applications up on the Web! Once you get past the introduction, though, there are a lot of useful problem/solution presentations.