12 lines of code
Given the plethora of Rails samples around, I was not sure it made sense to have one more "Hello Rails" application. On reflection, this will serve as a good launching pad to talk about other parts of Rails once we do the obvious and trivial.
Never let it be said that I’m not all for the obvious and trivial…
Starting off, let’s assume that you have Rails installed. It’s easy and there are clear instructions here: http://rubyonrails.org/down. You will also want a database server, MySQL is easy enough to grab and install. Once this little bit of housekeeping is taken care of we’re ready to have some fun!
I go to a lot of meetings; it’s the bane of my existence. In meetings we often make decisions, assign action items and report the status on things. At best, someone will take notes and email them out after the meeting so that they can be preserved for eternity in my inbox. I’ve often thought that a simple web app that would capture this information, track action items and completion, and make the information searchable would be pretty handy. So, let’s build one.
From the command line:
This generates the shell of your application, called "MeetingMinutes". Note that, among many other things, Rails has generated some stuff called models, views and controllers. If you’re familiar with the MVC pattern this should be familiar, it is the heart of a rails application.
Next, change to the "meetingminutes" directory which was just created and issue the ruby script/server command:
This starts the web server on port 3000. By default Rails will use "WEBrick", a lightweight Rails server; since I have Mongrel installed it’s using that. There isn’t any difference as far as we’re concerned.
Using a browser we hit this page and, lo and behold, we have a placeholder. At least it points us in the right direction
What is the time? OK, we’re about 1 minute into our development processes, let’s quit fooling around and write some code. At least we didn’t have to worry about XML configuration files!
A Rails app is structured around Models, Views and Controllers. The model represents the data entities in your application domain. For our "meetingminute" example this will be things like "meetings", "attendees", "agendas", "action items" and maybe a few other things. The views present the information, and the controllers provide the functionality necessary for the views to get data and manipulate it.
Let’s start by letting our users record meetings, then we have some functionality to show off and we can use that to solicit feedback and new requirements. We’ll want to create a model for our meetings and we’ll need to tell rails what a meeting is. In the Java world we’d typically do this by writing the DDL (SQL) statements to create the table, configure Hibernate or whatever our OR mapping tool is to point too the table and understand what the columns are, set up JDBC connections, perhaps in a JNDI server… Let’s see what is required here.
This generates meeting.rb, which is our model and the mapping from our DB table to Rails. It also generated a stub of a test harness for the unit tests, and created a "migration" called 001_create_meetings.rb. Small digression, Rails can work with existing tables, tables created through SQL, or it can use the concept of "migrations" which allow you to have a versioned schema and incrementally change your physical data structure. We’re going to go with this approach. To get to the point where we have an actual database table, an OR mapping and some web CRUD forms we need to:
- Edit the migration to specify the columns
- Configure the database
- Run the migration
- Create a view
Let’s just do that now.
Edit the migration
Open 001_create_meetings.rb in a text editor, and add the 3 lines as shown below. Notice that the migration lets you define the upgrade and downgrade commands, in Ruby, to modify the tables.
Configure the database
Actually, "configure" is a little strong. Two things to do. Edit config/database.yml to add the username and password that will be used to make the DB connection. Then use the MySQL command to create the empty database.
Notice that in this file it has pre-configured development, test and production environments. We’ll just be working with "development" for this example. Add the password and save the file. Then from the command line type mysqladmin create MeetingMinutes_development, which is the name of our development database as shown in the .yml file.
Run the migration
Now we’re ready to create the database table.
The rake command is a "ruby make" of sorts. What is just did was to look at our database, determine which version of the schema it is, and run the necessary migrations to bring it up to the most current revision. You can specify the schema version of course, but since the DB is empty it will run all one of our migrations. If you were to look in the database you would see that it created our table and columns, plus an ID column and a schema version table. We’re almost at the point of having a running application. About 3 minutes so far?
Create the view
Now we need to create a view so we can try interacting with our "application" Rails provides a handy thing called "scaffolding" that will give you a quick and dirty forms interface to your tables. Scaffolding can be generated from the command line with a script, or it can be "dynamic scaffolding" where it is created at runtime. We’ll use the later approach. Scaffolding is just a temporary measure, as you actually start creating your own custom views you will gradually replace the scaffolding. Yes, you can replace it piecemeal, which is very handy. Generating the scaffolding from the command line is useful too, then you can edit the generated code. We’ll look at that another time.
We need to create a controller first:
Note that again it created not just the controller, but also a test stub. We’ll need to look at that another time. Edit the controller meetingminutes_controller.rb, and add the single line scaffold :meeting:
Start the server again and browse to http://localhost:3000/meetingminutes, you now have a set of forms that let you create, delete, edit, show and list records in this table! Scaffolding is not the end, just the beginning, It gives you a way to quickly prototype and review. The cycle time to make changes in Rails is *really* fast, and the results are immediately gratifying.
We’re maybe 10 minutes into our development. We haven’t gone deep, but we have a functional (if shallow) application. One thing that you should look at is the actual model file. This is where the object-relational mapping magic happens.
Yes, that is it. ActiveRecord takes care of all of the O-R mapping dynamically. If you add a column to your table it is already there for you to use in the model. If you change a column type, the changes just ripple through painlessly. Just for fun, type rake stats, you will get a quick anaylsis of the LOC, classes and so forth in your application. So far we have 12 lines of application code.
This doesn’t even scratch the surface of Rails, but it gives a decent flavor of what it is about. We’ll pick up development on this application soon and see where it takes us.
Share This | Email this page to a friend
Posted by Joe McGlynn on January 3rd, 2007 under Ruby and Rails |

RSS Feed

January 4th, 2007 at 9:39 pm
Joe,
This blog is really a nice introduction into rails!
I really don’t want to be destructive or something and I also know that there are functional and conceptual differences, but have you ever tried to setup a hello world Web application with IntraWeb?
It’s basically:
o Delphi-File-New-IntraWeb Application
o drop Label on form
o change Label.caption to "Hello World"
o [F9]
That are zero lines of code, and you end up with a self contained application server. If you prefer ISAPI or .NET assemblies, its just an additional mous click in step one.
Ok, no DB access, but thats done like you know it from Delphi:
- drop Query + DataSource
- drop Grid and connect
- [F9]
3 more steps. Still no code
Ah, ok there is no "Model". Do you know ECO? The next Delphi/IntraWeb version will have a button "IntraWeb Eco application". I’ll make a preview available on my blog
Regards,
Olaf
January 4th, 2007 at 11:14 pm
I like seeing Ruby on Rails coverage on a CodeGear blog, nice to see that eyes are looking out across the landscape.
Personally I’ve always really liked the RoR combo, it feels like a tool that does what it was meant to do well. Whenever I start a new web project I’m always torn. RoR is great for small/medium (and probably large) web sites yet the ASP.NET framework is amazingly rich but cumbersome at times.
The IntraWeb + Delphi + ECO combo feels "heavy" while Ruby on Rails feels "light". Probably a strange perception but that’s what I get from it. I think it comes from the fact that RoR isn’t trying to be anything more than what it is, it is focused on being a MVC-centric, ORM-backed framework for creating dynamic web sites.
RoR also seems to embrace the nature of the web itself, being dynamic and easily updatable and changeable. Get a call from a client that needs a survey or feedback form coded up pronto while you’re on vacation and only have access to your teen nephews computer? No problem, ftp, grab the files, make the changes in notepad, use migration, rake, etc. and your’re good to go. Try doing that with a .NET or IntraWeb/ECO/Delphi application when all you have is notepad and a slow connection.
Sure, you could always do that (minus the migration) with Perl, PHP, old-school ASP, etc. but unless you were very diciplined those tended to easily become muddled, non-OOP feeling and out of sync with the DB schema.
Anyway, love to see the Rails posts, it’s refreshing.
January 5th, 2007 at 9:44 am
Looking forward to see what, if anything, CodeGear does with RoR…
January 8th, 2007 at 8:42 pm
Olaf,
You’re right in that there are many way to do code-lite or code-less applications. At the same time I think RoR has some distinct advantages.
It doesn’t try to eliminate code, just to eliminate unnecessary code (and configuration). Small, expressive code that can be easily read and understood (by someone familiar with the dialect).
The lightweight-ness of RoR doesn’t diminish as the app gets bigger or more complex.
Watch as we add additional tables, relationships and Ajax. It maintains the same crisp, terse approach.
Of course it has the same cross-platformness of a Java solution, and a large and rapidly growing community of extensions (just for example - substruct, a complete ecommerce website framework which is a Rails plug-in; electronic payment modules for various merchant services, Rails blog framewortks…)
There is no silver bullet, but RoR can be a powerful arrow in your quiver
January 10th, 2007 at 6:22 am
I tried to look at your posting identified in the Title above. The link returns the below error:
///////////////////////////////////////
.Text - Application Error!
Details
String was not recognized as a valid DateTime.
Return to site
///////////////////////////////////////