2009-01-19

Getting Started with Rails

I've recently had a few inquiries about how to get started writing web apps with Ruby on Rails so, in order to repeat myself less, and in general, congeal my thoughts, I blog:


(1) Learn Ruby.

Unless you're a Smalltalk &/or Perl whiz, and instantly absorb unusual programming syntax, Rails will seem like black magic without a clear understanding of how Ruby works, and what it is capable of.

Ruby is a non-compiled (scripting) programming language. It's very plain-english in style, and, from what I read, draws upon some of the best practices of Smalltalk and Perl. Yes, Ruby comes from Japan. Yes, everything is a first-class object. Yes, Ruby is a scripting language. No, it doesn't compile. Yes, it really is cross-platform. And, yes, it does totally rock.

Go to the source and read a bit. Wow yourself. Then go buy a good book. I recommend the PickAxe book from Pragmatic Programmers.

(2) Pick a working environment.

A key to getting your work moving will be to develop a stable workflow. (I hesitate to use the term IDE, because I associate it with big clunky develop-crap-ware, like Visual Studio and Eclipse. Yuck.) Ruby is extremely flexible here, leaving you plenty of choices from which to select a good fit. So do so.

If you like to work online, Heroku/Heroku Garden are your workspaces. If you think GMail is a 1st class email client, Jungle Disk is the best removable media you've ever used, or subscribe to the Bungee Labs mantra 'Build the App, not the Crap', then Heroku is your new best friend. Heroku is built around a web application for developing and testing Rails applications. What about deployment? Get over it. It's waaaay easier than you think. BTW, your test app is publicly deployed, as you develop it, if you so choose. ;-)

If you like IDEs, and took serious offense to my prior parenthetic remark, please please download NetBeans w/Ruby. You'll do just fine. Or, if you're primary OS is Windows, NetBeans is for you, too, because command-line work in Windows still isn't any fun. NetBeans will let you run projects either on the built-in JRuby, or any other local Ruby install. If you plan to do something freakishly wacky, like run Rails apps on a Java App Server, this is also the place for you. Otherwise, install Ruby and switch the Netbeans default platform over to it.

If you worship Steve Jobs, then take out a withdrawl from your Mac Tax account and iBuy your iSelf a copy of Textmate. That's what all the cool iKids use.

What do I use? NetBeans on Linux. Until it annoys me, then I go back to gnome-terminal and gedit. I use a custom script (called grails - yes I do like to gname gthings on gnome) that launches gnome-terminal with 3 tabs (a web server in development mode, a ruby console in development mode, and bash in my project folder) as well as gedit with the folder view rooted on my project, with the database schema & the README open (one of these days I will read it...).

(3) Commit to learning Rails.

Don't even think about all the cool things your first Rails app could do. Stop drooling over how many plugins you could use. Get a book. Take a class. Run some demos. Write 50 Hello World apps. Learn how it works before you even think about making it do what you want. Think of it as investing in your Karma. If you go into your first Rails app thinking it will be anything useful, from an amazing .com startup to a little toy for your spouse, YOU WILL NEVER DEPLOY THE APP.

If you're going the NetBeans route, Sang Shin has a course that covers all the bases, and all the basics, and puts you through the Hello World wringer. Pay attention, stay on schedule, and turn in all your homework and you might just walk away with a Certificate in Rails for your Ego Wall.

If you're bookish, go get yourself a copy of The Rails Way, by Obie Fernandez. Kiss the cover. Sleep with it under your pillow and hope for osmosis. This book is awesome. Amazing. It will knock your socks off. And if you're wondering who this Obie guy is, he's serious. He's got a nice little company that walks in and builds production Rails apps from scratch in 3 days!

As strongly as I recommend TRW, I recommend avoiding Agile Web Development with Rails. This was my first Rails book. I thought it was pretty good, but that's only because I didn't know better. It misses many of the finer points of Rails completely, and although it does walk you through building a demo app, the implementation leaves much to be desired. Besides, the Depot App is included in NetBeans, for your study pleasure. In my opinion, covering SQL design in the MySQL console, instead of Migrations, is neither Agile nor Rails-ish.

(4) Continuous improvement requires continuous absorption.

Subscribe to blogs. And podcasts. Read wikis, and READMEs and newsgroup archives. Google 'Rails anything' and see what other people did. Someone smarter and more experienced than you probably wrote a tutorial on how to solve your Monolithic Problem with 3 lines of code.

In no particular order:
Also, when you're ready for some serious 'a-ha' moments, get yourself a copy of Design Patterns in Ruby by Russ Olsen. Imagine: you can learn design patterns in a OO language without having to wade through miles of C++ or Java code, and learn a few Ruby-only design patterns. Prepare to truly grok how Rails works for the first time.

(5) Teach your app cool tricks.

That problem that you're thinking about solving - the one you just filled the whiteboard sketching out - someone else solved three years ago. Yes, there's a plugin for that. Maybe a whole gem. Google 'Rails myproblem' and you'll find a million solutions. Afraid you're going to muck up your work? Copy the whole project to another folder, then install the goodies there.

(6) Get comfortable with a Revision System.

Subversion. Git. CVS. Mercurial (woot!). Whatever. Pick one. Use it. Again, if you're one of those 'living in the clouds' folks, you should have no problem finding a hosted revision system, especially if you're going to build an open-source app. Check out Github for Git, or Sourceforge or code.google.com for SVN.

(7) Deploy your app with robust resources.

Don't expect a $5/month hosting account to handle your app well, even if you're the only one using it. Starting up a Rails app is system-intensive. If you are using hosting, I'd recommend a VPS to start with. Or your own server if you've got one. Bandwidth is usually much less of an issue than available CPU cycles.

Slicehost offers solid VM hosting starting at $20/month, and it should do fine for your startup app. I default all my new app VMs to 256 megs of RAM and 1 CPU, and flex them up when there is demand.

If you're going to host yourself, check out Jumpbox's Ruby on Rails VM Appliance, configured excellently on a Ubuntu server.

If you decide to build from scratch, don't get hung up on old software, just because its there. I run all my production apps on mongrel, the same server I develop with. I use either pound (pure reverse proxy with an HTTPS wrapper) or nginx (if I need to serve a bunch of static content, and don't want to tie up my mongrel cluster doing it) as a reverse proxy, depending on the apps requirements. Both have super-low requirements, super-easy configs, active developers, and good behavior.

That's all, folks!

If I missed anything major, I'll do a follow-up. Feel free to add your own best practices in the comments!