I own several books on Rails, all of them very well written and pleasant to read. But there’s a big difference between reading a book and knowing & learning a platform. You have to code in that platform, you have to first build something trivial, and then build at least one thing that’s non-trivial. Only then do you start to become intimately familiar with a platform’s conventions, the APIs and its general approach to problem solving.
With that in mind, I present a basic guide to how I learnt Ruby on Rails at a steady pace and without feeling overwhelmed by its enormity.
Some background reading
It helps if you know the a few of the basic concepts of Rails. I think any developer can pick up and run with the following steps, but it’ll make more sense if you’ve skim-read the rails website, watched a screencast of app creation, or glanced at the basic language syntax.
Start with something very simple
As the eventual aim of using Rails is to create a webapp, start by designing the layout of a couple of HTML pages. They don’t have to be pretty, but they should be related to something you’re interested in rather than a generic blogging engine.
- Create a few basic pages in HTML with a text editor and test that they look as you’d expect.
- Read up on Action View, (start with the Rails docs page).
- Create your first Rails project. You can do this if you have rails installed by opening a command prompt and typing ‘rails myFirstProject’. However, I’d recommend you leave the command line behind and try Netbeans. Even if you don’t normally like using an IDE, Netbeans have made it very easy to get started by packaging up a Ruby-only edition. It’s only 22meg to download and includes helpful features to ease the learning process, as well as useful features for webapp developers such as CSS, HTML and XML assistance. You can download it all for free on the Netbeans download page.
- Once you’re familiar with Netbeans and have created a Rails project, create your first “controller”. We’ll use this controller as the central point for all your prototype pages to hang off. By default it will also be used in the URL of your prototype site. So pick something simple like ‘main’ or ‘admin’ depending on the kind of mock-ups you’re going to create.
- Import your basic HTML prototype pages into your project – create an action method in your controller named something like ‘welcome’ and then copy your HTML page into the views directory for this controller. Name it welcome.rhtml.
- “Run main project” from Netbeans to get a ruby web server working. Verify that your Rails setup appears in a browser window – if there are errors, you may have to tweak the configuration in config/database.yml.
By now you should have 2 or 3 web pages with links between them, and a rails web server should be running. This will mean your prototype pages are served up by a real web server. You are the only one that can access it for now, but it’s an important step in understanding the full development process in Rails.
If it becomes difficult to run your project inside Netbeans, you can drop to a command-line and run webrick (the pure ruby web server) manually. Launch ‘script/server’ from your rails project directory.
Convert your header/footer into a Rails layout
Abstract a few little things out of your templates; read up on the ‘layouts’ feature of rails so that you can start to add interesting menus and footers to your prototype. Once you have a rails layout, you can start using rails helper functions to make your templates more interesting.
Go mad with CTRL-Space in Netbeans, read some of the descriptions, (but not too many) in the auto-complete when you’re writing your RHTML pages. This should start to deepen some of your understanding of the functions available to rails developers. While it’s not especially hard to figure out what the link_to function outputs, by skimming the descriptions every time you use it, you can start to tap into its advanced power and flexibility.
Start to build some rudimentary headers and footers into your site using rails layouts. It’s a full app now, even though it’s only for prototyping. You can now add some more interactivity to your user interface and analyse your webapp to see how viable the concept is.
Use CTRL-shift-O to open files, get a feel for the Netbeans default key strokes. Explore the ‘refactor’ menus. Hold down the CTRL key (or apple key on a Mac) while you’re looking at a page that has ruby expressions. As you hover over the ruby functions, they should turn into hyperlinks. If your ruby installation is configured correctly, you can click these dynamic hyperlinks to browse directly into the rails source code and have a quick look around.
Check the preferences in Netbeans, adjust a few things to your liking. We might only be making a prototype, but seeing as Rails make it so easy to turn it into a real app, it’s best to keep everything neat and tidy from the start.
Auto-complete with style
When you’ve started getting some decent prototype pages together, think about enhancing them with a few dynamic elements – nothing too coding intensive – just enough to help you get used to putting “logic” into the controller instead of the template.
For example, you could add the current date to the header of your page with a simple expression:
<%= Time.now %>
Game, Set and Match
It’s worth taking a break from your Rails app at this point, and getting to know Ruby a bit more intimately. If you’re feeling adventurous, I’d recommend you write a small game engine. Before you run screaming, this has nothing to do with graphics; the important thing is just some basic logic – the sort of thing you might have done when you were first learnt how to program. Pick a game with rules whose complexity match your available time – 21 is a very simple game, bridge or poker are trickier, snooker or pool might take a bit of work.
It might be tempting to skip over this exercise, especially if your time is limited and you just want to get a simple webapp setup ASAP in this framework. But this is the part where I really started to ‘click’ with Ruby; to get a feel for what works and what doesn’t. There are no web-related distractions when writing pure classes. It does help to have a book on hand here, (see below), but google is often faster than manually looking stuff up in the index.
The important thing is to write some simple methods that contain very little except pure application logic. This helps you to interact with the property accessors of Ruby to learn how to write well structured code with this dynamic language. If you’re really eager, (or you’re taking this Very Seriously), you can also write some test cases to really prove to yourself that your logic is working for all possible cases.
Take hold of ‘irb’, the Ruby interactive interpreter, and play around with your new classes. You can get rails to load your classes into the interpreter automatically by executing the ‘script/console’ command that is part of your rails project.
Expect some errors, but if you keep the scope of your program limited, they should be easy to track and fix; you’ll only be using the core features of the language. Make sure you’re familiar with lists and iterating over them (using ‘each’ and the block syntax), Ruby symbols, hashes and get to know the language limitations. I learnt that Ruby doesn’t support method overloading but you can create optional parameters by assigning default values to method parameters (like in C++). Similarly, I was surprised to find that there’s no language-level support for enums, but you can workaround such issues with lists and symbols.
In part 2 of this article, we’ll go over the basics of adding database connectivity to your app and setting up a user/login system. In the meantime, get hold of Netbeans from their downloads page and grab a copy of one of the great books on working with rails.