This article doesn’t teach you a lot about Ruby or Rails, but the nice part about it is that it shows you how easy it is to build web apps with Rails.
I went through his tutorial last night and looked at my watch before and after. Literally, it took only 20 minutes to build a MySQL-backed bookmark storage application and I spent a good chunk of that time trying to figure out why it wasn’t working for me at first (contrary to what the article advises, I had to launch the WEBrick server while in the “Bookmarker” directory) and trying to analyze what was going on behind the scenes. Pretty impressive that you can build an app that fast and without writing a lot of tedious “glue” code
Here’s a brief summary of the steps:
- Create a database (I used MySQL) and a table. Rails wants you to create a table for whatever object you’re trying to store with a plural name (e.g.: “bookmarks”) and wants it to have an auto-incremented integer primary key called “id”.
rails <app_name>to have Rails generate a directory for your application with 11 subdirectories and lots of files already filled-in for you.
config/database.ymlto point to your database.
ruby script/generate scaffold <model_name>to generate a bunch of code that handles New, Show, Edit, and Destroy operations.
model_nameis your database table but with a capital letter and not plural (e.g.: “Bookmark”), which I found to be weird and inconsistent.
ruby script/serverto fire up a WEBrick server on port 3000. It logs to stdout so it’s very easy to see your errors.
- Point your browser at
http://localhost:3000/<controller_name>and observe that you have a working application! (
controller_nameis like your database table, lowercase and plural – e.g.: “bookmarks”).
- Customize and extend the pre-generated code in the
appdirectory. Ruby code has a
.rbextension and HTML templates have a
.rhtmlextension and live in