Building a Ruby on Rails application in a week

June 9, 2008 by Rasmus

This post will (and perhaps some followups) describe how I did a web application in a week using the web framework Ruby on Rails

My company 41concepts is currently developing a rather large application using Ruby on Rails and during this I had a week where I had to wait for something being delivered by another developer. The application wasn’t really supposed to grow this large, but the feature list just seemed to grow the more we developed.

Because I had this week, I wanted to try to make a small Ruby on Rails application implementing an idea I’ve had for some time now.

I really like my Playstation 3 and if you ask my girlfriend this is also a bit too much sometimes. I generally like adventure and roleplaying games such as the Final Fantasy series and because I hate wasting my time, I usually follow a game walkthrough.

With this application I really wanted to give something back to the game walkthrough community, so I decided to create the site/application “My Walkthroughs“. The main idea is to let users of the site create their game walkthroughs and then share them in different format such as pdf, plain text and html. The idea comes from the fact that almost all walkthroughs are being done in plain ASCII text.

So the application should enable users to build a walkthrough through a web interface and publish it in various formats.

Ok… I had a week for this, so what did I do?

Planning

Well.. I must admit I went straight to implementation. This was a small site and really just a pet project.

Implementation

Design

I’m not really a Photoshop shark (I don’t even have Photoshop), so I found a free template:
http://www.opensourcetemplates.org/templates/preview/1303028527/

For icons I used the FamFamFam silk icon set that is simply astonishing:
http://www.famfamfam.com/

Finally I decided that I wanted some kind of Logo, so I invested in one from Logo Samurai:
http://www.logosamurai.com/

Cool css scalable buttons:
http://monc.se/kitchen/stew/buttons/btn.html

Took the confirmation boxes from:
http://www.templamatic.com/blog.asp?BlogID=18

Got the cool form hints from here
http://www.askthecssguy.com/2007/03/form_field_hints_with_css_and.html

Code

I used a lot of different plug-ins and gems. The fastest code to implement, is code you don’t write :). They are listed below:

Plug-ins

acts_as_bookmarkable
Suddenly I thought it would be a great idea if the user could bookmark other user’s walkthroughs

acts_as_commentable
Of course users should be able to comment each others walkthroughs, it wouldn’t quite get the web 2.0 feel without :)

acts_as_list (tutorial)
This is used for lists that should be sortable

acts_as_rated
This was added late, it turned out really nice with this tutorial, adding nice css/ajax support. Also note that all the averages are calculated when the entity is saved and not at render time

attachment_fu (tutorial)
This is used for all uploads and thumbnail generation. It works perfectly together with Amazon S3

auto_complete
Used for completing the game’s name when creating a new walkthrough

restful_authentication (basic use, great screencast)
The standard on authentication

simple_captcha
Used for registrations and other places where spam bots like to date

synch_s3_asset_host
This was used in the Capistrano recipe for synchronizing the public folder with the Amazon S3 asset hosts

Gems

htmldoc (tutorial)
I had to modify it though, when certain PDFs was generated, the gem reported false as return value but left nothing in the error collection. After some investigation I found out there was a bug in how the result from the command-line was parsed and changed line 186 from “case line” to “case line.strip” since there was some \r characters that the code didn’t quite expect. I’m not sure if this is only applicable to me, but now it works

mini_magick
Image manipulation for use with the attachment_fu plugin. This is not as heavy as the full rmagick since it uses the commandline. I wanted to use this with the simple_captcha plugin as well, but it can only modify existing images, not create new ones.

rmagick
Image manipulation for use with the simple_captcha plugin

aws-s3
Provides a nice api for Amazon S3

uuidtools
Used for generating GUIDs. Notice that it couldn’t find the MAC address on my virtual server at Slicehost, so I had to manually give it a MAC address

will_paginate
Great for pagination. If you want to use ajax paging - something we use in another application - this works perfectly

In total the model folder in the application consists of 11 models, 3 observers and 2 mailers. A small and simple app :)

The application use REST and search engine optimized urls everywhere which was something I really wanted from the start. Take e.g. the url for a walkthrough:

/walkthroughs/123456-resident_evil_5_ps3/summary

On this page the user gets a summary of the walkthrough and can download the walkthrough in three different formats using the URLs:

/walkthroughs/123456-resident_evil_5_ps3 (no extension => html)
/walkthroughs/123456-resident_evil_5_ps3.txt (plain text - ascii)
/walkthroughs/123456-resident_evil_5_ps3.pdf (well… pdf :))

The code looks at the last edited time of the walkthrough and generates a new one if needed. The output is placed on Amazon S3 and the user is redirected to this. It would be a bit nicer if the application streamed the file from S3 and then directly on to the user, but in order to save server resources, the user is redirected directly to the S3 url (which is currently not that nice).

Deployment

We are currently trying to do deployment on Amazon EC2 for our larger application, but with this one I really wanted to try out the new Passenger (mod_rails) Apache plugin, since it promised real easy rails deployment.

So I bought a slice over at Slicehost and pretty much followed this tutorial to the letter. It worked like a charm.

After this I set up Capistrano according to Slicehost’s own Capistrano documentation.

I also signed up for the Amazon S3 storage service for the walkthrough pdf and txt files, as well as the user avatars (from attachment_fu plugin). Furthermore I used the synch_s3_asset_host to synch the public folders with asset hosts in Amazon S3 during the capistrano deployment.

For tracking I added the site to our Google Analytics account.

Aftermatch

Well, I must admit that not everything was actually done in this one week. The following was added after week one:

  • The domain name (I forgot to start this process in the beginning of the week, and it takes a couple of days to process)
  • The logo from LogoSamurai took some days with revisions and all
  • The bookmarking functionality
  • The asset hosts in the capistrano recipe
  • Forgot password functionality (don’t know why this slipped in the first place)
  • Some minor bugfixes to the pdf and plain text rendering (I will probably keep improving this)
  • Caching (this is something I will need to monitor as the site gets traffic)
  • I normally write alot of tests, I must admit that I could do better with this app :)
  • I’ll probably optimize some database indexes along the way
  • A nicer system admin area using Active Scaffold or similiar

Price

So what did all this cost me!?

  • Hosting @ Slicehost $20 a month first bill was $60
  • Logo (LogoSamurai 3 designs unlimited revisions) $147
  • A bunch of great rails plugins - FREE (thanks…)

$207 and one week of work (plus some follow up described above).

Now I only need to get some users on the site (the hard part). I might write a bit about the bootstrapping process later.

So go to http://www.mywalkthroughs.com and give it a spin.

Man I love rails…

Introducing Railscheck - A Q/A verification tool for Ruby On Rails

April 15, 2008 by mmc

The problem

It is a law of software development that the earlier one identifies a problem the cheaper (and less embarrassing) it is to fix. Hence, we developers strive to identify errors early on our own development machines aided by sound development methodologies, tools, and tests (at best automated).

A particular quality assurance challenge with dynamic technologies like Ruby/Rails, Javascript or view templates like ERB is that you have no compiler that checks for syntax errors in advance. So you have to have 100% test coverage (which is often too expensive to be realistic) in order to be assured that even simple syntax errors does not occur.

Furthermore in the case of Ruby On Rails, which is of heavily opinionated nature, there are many conventions that can be broken by mistakes, which may introduce errors that do no show up initially. This introduces additional testing/debugging work for the programmers (in particular for newcomers to RoR).

Happily Ruby On Rails is so productive a web technology that it more then compensates for these difficulties but even though would it not be nice with some automated tool to help out a bit?

Unfortunately, your typical Ruby development tools does not provide any kind of static verification checks since the problem appears to be just about unsolvable in the general case (because of the dynamic nature of Ruby).

But what about the smaller problem of static verification checks for Ruby On Rails projects only? The RoR domain is much smaller and highly standardized so what is impossible for the entire domain of Ruby programs should be partly possible in Ruby On Rails!

Railcheck

So inspired by LINT , the power of Ruby meta-programming and various RoR testing snippets on the web, I wrote this initial beta release of Railcheck which is a semi-static verifier for your Ruby on Rails projects.

Delivered as a free Ruby gem the Railscheck project provides a shell command task “railscheck” that you can run against your Rails projects to test for a number of typical bugs and inconsistencies. See the project site and linked readme file at railscheck.rubyforge.org for details about how to install and run the tool and much more.

Railscheck is a working beta. The gem works and is useful but has a limited feature set. Much more to come. Use the rubyforge tracker function on the Railscheck website to suggest features that you would like to be added (or bugs to fix). You are also very welcome to add code/tests to the project, which is open source. Indeed the project is now open to new contributing members.

Update:

Updated the gem with improved contributor documentation, an explicit gem dependency and a fix for a typing error in railscheck.rb file. Try again if you had problems.

Rails and file uploads

April 10, 2008 by Rasmus

While there certainly are several good file upload plugins for Ruby on Rails. It is actually somewhat hard to find any good tutorials on how to do it yourself. I just wanted to post to great links that helped me figure the whole thing out :)

http://blog.vixiom.com/2006/07/26/rails-stringio-file-upload/

http://manuals.rubyonrails.com/read/chapter/78

(Now I also know where to look when I have to work with file uploads in the future)

Ruby Fools conference in Copenhagen

April 3, 2008 by Rasmus

So we went to the Ruby Fools conference here in Copenhagen and this post kind of sums up my experience of the different speakers. The conference went over two days and had three tracks. I primarily focused on the Advanced Rails stuff. I’ll probably update this post once you can download the presentation slides (and video).

Tuesday:

Dave Thomas explained in his opening keynote why he is a Ruby Fool :). He’s gave a great performance about his passion for Ruby and went on to compare the sudden rush of developers coming to the platform with Rails to “golddiggers and prostitutes” (before Ruby was this nice little settlement, and suddenly everyone wanted to join). That of course hit pretty much 80% of the people sitting in on the keynote. Great stuff :D.

REST: A pragmatic introduction to the Web’s architecture by Stefan Tilkov. While REST is not that new to us, and the speak therefore didn’t provide much to us, Stefan was great to talk with and I threw a few ideas on him about some of our challenges with REST (like, what do you do when you’re implementing a dashboard with some functionality also found in other places of the system - expect a new blog post on this).

Tuning the Rails stack by James Cox. Since tuning involves turning alot of knobs al over, this is not an exact science, but he did give som nice pointers on e.g. MySQL tuning and also told a few scary stories on applications that didn’t scale. On a side note, he had the coolest presentation slides (well, not really slides, I think he said it was a flash movie :))

Advanced Ruby on Rails security by Heiko Webers. I’m having my doubts on what to write here. Let me first say that the content of the presentation was great and very “german”. It was “Do this/Don’t do this”. Great stuff. No room for interpretation and once the slides are downloadable I’ll probably run through every slide while looking on our own TBA application :). Unfortunately Heiko wasn’t the great presenter and while the slides was clear, he pretty much just read them out loud, but was in trouble whenever he had to explain something that wasn’t on them.

Meta-meta programming by Nic Williams - man I love this guy.

1) The presentation was close to useless when looking at the use cases where this can be applied.
2) The subject was VERY technical

And yet… This was perhaps one of the greatest presentations due to the amount of humor and general relaxed style of Nic. Basically everyone knows meta programming, so meta-meta programming was the meta programming of meta programming. The easiest explained example in rails terms is a generator that generates generators (as I said… Not the most common use case :))

Party keynote by Evan Phoenix. A couple of sponsors had provided food and beverages so of course there was a “party keynote” (Dave Thomas wanted to swap keynote with Evan :)). While I have a great respect for Evan, the keynote was close to “not relevant at all” to me. He basically explained how he does project management on the open source project Rubinius. I’ve haven’t been in a open source project before, but what he presented was pretty much “be nice and positive to people”. There was some debate about the policy on the project that once you committed your first patch you get full commit access to the project. While it certainly works for him, I’m still having my doubts.

Wednesday

Keynote: Ruby: Past, Present and Future by Yukihiro “Matz” Matsumoto. This whas great to here it from the creator himself. There weren’t many surprises about Ruby it self, but it was great to hear about what his thoughts was on other languages and why he then went on to create Ruby. The future part was a bit cutted off because he spent a little too long bitching about character encoding (UTF-8,16,32). It was clearly something that he’s spent a lot of time on with the 1.9 release.

Versioning your data model by Ole Friis Østergaard. The presentation explained 4-5 different plugins that had something to do with versioning (also an undo redo plugin that actually looks rather nice) - including his own new plugin Subversive. I kind of noted that this was actually one of the first presentations that actually showed “real live code”.

Adding full text search to your Rails application by Jørgen Erichsen. We’re currently also implementing the search enginge Solr into our own application so this presentation was a must see. While Jørgen went through the basics of the search engine and the acts_as_solr plugin (as well as Ferret and also briefly mentioned a couple of other solutions), he didn’t seem that knowledgeable about the subject when getting to stuff outside the basic behavior of the product.

The dark art of developing plugins by James Adam. This was great. James presentation was very pedagogical buildig a plugin step by step explaining every bit of the way. While plugin development aren’t that difficult to grap, it certainly put one or two thing in place for me. I’m looking forwards to his slides so I can wrap our authorization for our application up in a plugin.

After this I unfortunely had to leave because of another engangement. So I missed one speaker as well as the ending panel discussion.

Overall the conference was well planned and executed. You could perhaps argue that it is limited what you will learn in one hour presentations, but I could just have attended the workshops leading up to the conference. As a last note… The track introductions seemed a bit off = 30 minutes break - 15 minutes track introduction - 15 minutes break (though Glenn Vanderburg was quite good.

Shrink your Windows disk image on VMWare Fusion (Mac)

March 31, 2008 by mmc

It is possible to free up quite a bit of hard disk space by compacting your virtual disks for VMWare.

Here is a easy (manual) way to shrink a Windows guest image hosted on VMWare Fusion for Mac OS. I have used the recipe below to successfully save about 50% of disk space for a Windows 2003 R2 guest image on Mac OS 10.5.2 running VMWare Fusion 1.1.1. Other Windows guest OS’s like Windows 95, 98, NT 4.0, Win2K, XP should work with this recipe as well, although this is untested at this time.

Warning: This recipe may/may not work for you. Beware of the risk! Remember to backup your disk image before messing with it. Finally, do not shrink a disk with snapshots as this is known NOT to work!! Update: See additional recipe at bottom of this post for how to deal with snapshots.

Shrink recipe:

  1. First backup your guest os disk image and other files (just in case something goes wrong) + make sure you have plenty of spare disk space on your harddrive.
  2. Start Windows guest OS in VMWare Fusion and login as administrator
  3. Clean up & defrag guest OS disk image from within guest OS
    * Delete all unused files, empty your trash can, delete your browser cache etc.
    * Defragment your guest OS (f.x. using “defrag c:” in the cmd prompt).
  4. Zero out unused disk space in guest OS disk image
    * Download Mark Russinovich’s disk erase tool SDelete v1.51+ and place it on the local guest drive (*1).
    * From a command prompt navigate to where you saved the above delete tool and write “sdelete -c c:”, which will clear all free disk space
  5. Close guest OS and VMWare
    * Shutdown your Windows guest OS.
    * Quit VMWare fusion
  6. Shrink disk image using VMWare’s diskTool
    * Navigate to the VMWare Fusion tools directory. E.g. type “cd /Applications/VMware Fusion.app/Contents/MacOS”
    * Run VMWare Fusion’s diskTool command with the shrink option “-k” to compact your virtual drive (*.vmdk). E.g. type “./diskTool -k 0 full-path-to-your-virtual-desk-image.vmdk”. (*2)
  7. Finished!

*1) Nb. this erase tool is not documented to be compatible with Windows 2003/XP, but it works just fine on on my Win2003 R2 (as far as I can tell).

*2) Do remember the “./” when running diskTool to avoid mis-executing the Mac OS disk tool with the same name instead.

UPDATE - QUICK SNAPSHOTS CONSOLIDATION TIP:

If your disk image has snapshots you need to consolidate those into one large disk before attempting the above recipe. You can create a single disk image by cloning (merging) your disks using VMWare’s diskCreate tool (located in same folder as diskTool). The procedure is:

  1. Again backup your stuff first in case things go wrong (and again make sure you have plenty of spare disk space).
  2. Create a new folder with the extesion .vmwarevm to hold the output files.
  3. Navigate to the VMWare Fusion tools directory. E.g. type “cd /Applications/VMware Fusion.app/Contents/MacOS”
  4. “./diskCreate -C path-to-latest-snapshort-vmdk-file.vmdk path-to-new-merged-disk-file.vmdk” (use output folder above for destination).
  5. Copy the .vmx , .vmdk and .vmxf files into the output folder. Use a texteditor to edit the .vmx (and .vmxf) files to reflect the new disk file name.
  6. Test that the new consolidated image works in VMWare fusion.
  7. You can now proceed to the recipe for shrinking your disk.

Using ExtJS tree on rails with the prototype adapter

February 22, 2008 by Rasmus

UPDATE: Notice that the new version of ExtJS is now on Prototype 1.6, yeah…

So I’m doing a folder tree using the ExtJS javascript with the prototype library adapter. I’m pretty much doing this.

This was somewhat working. Every third or forth browser refresh the tree didn’t load any nodes. Very odd indeed. I debugged my way through using Firebug, but when this was on, it worked everytime. Since Firebug slows down the entire javascript environment I concluded that the error occured when something (or loading) happened too fast.

In the ext-all-debug.js (version 2.0.1) I tracked the error down to line 23754. The json result being evaluated is undefined (notice that this only happens some of the time). So… after hours of head scratching and putting alerts in both the prototype library and ExtJS library to see what was wrong, it hit me…

The version ExtJS I have, come bundled with prototype 1.5.0 while rails 2.0.2 comes with prototype 1.6.0.1. In my layout I simply included <%= javascript_include_tag :defaults %>, but after I changed this to:

<%= javascript_include_tag “application.js” %>
<%= javascript_include_tag “ext/adapter/prototype/prototype.js” %>
<%= javascript_include_tag “ext/adapter/prototype/effects.js” %>

Everything seemed to work. Now my only problem is that I now use Rails 2.0.2 with an old version of prototype. So can I use all the rails helpers?!?

Installing Mephisto (multi site) on DreamHost

January 7, 2008 by Rasmus

The title might be a bit off, since I’m not providing you with a step by step guide. This post do however sum up my notes during the install.

I started by following excellent guide:
Mephisto and Dreamhost

After looking at several guides, this one was really the easiest to follow.

Now Mephisto was installed and I wanted to add multiple sites support and I followed the guide:
Setting up Mephisto for multiple sites

I did everything but couldn’t figure out why the site still used the site root (/public) as cache location. Well silly me. As it turned out I had to restart the fcgi process (something that I really thought I’ve already done). So… Remember to do a:

touch public/dispatch*

from the application root folder.

Installing RadRails and getting error “Requested operation cannot be performed because it would invalidate the current configuration. See details for more information.”

January 2, 2008 by Rasmus

Up until now, I’ve been using TextMate to create my Ruby on Rails code, but I’ve been missing stuff like autocompletion (I know, a bit more difficult when using dynamic languages, but I really miss it because I also do alot of C# development in Visual Studio).

So.. I decided to try out RadRails that is now named Aptana Studio and the Rails development package installed as a plugin.

After the download and the installation I then tried to install the Rails plugin, but got this strange error:

“Requested operation cannot be performed because it would invalidate the current configuration. See details for more information.” and “ Subclipse Mylar Integration (1.0.1) requires plug-in org.eclipse.mylar.tasks.core (0.9.2)”, or later version.”

After some searching I found the answer here. All you have to do is to un-select the Mylar integration. Although this might be a simple operation for experienced users of Aptana Studio, I was a bit puzzled about this. As someone who just wanted to try out the Rails environment (and I guess I’m not the only one), this seemed like an unnecessary complex installation (at the time I didn’t even now what Mylar was - now Mylyn)

Best of Ruby/Rails in 2007

January 1, 2008 by mmc

Thanks to the great work of the Ruby & Rails communities, the year of 2007 saw many exciting new software releases. I have listed a few releases that I would like to point out in particular and expressively thank the developers for:

* Rails 2.0 - A great open-source framework got better in December 2007 with the v2.0 release. In particular I like the improvements in security, test fixtures, http support, performance, debugger and the non-beta addition of ActiveResource by default.

* JRuby 1.0 - By mid 2007 the first proper open-source release of JRuby arrived and it was able to run (most) Rails applications. Very impressive but a bit beta-like (many key API’s are still changing and there is little documentation in the download). Look out for an even better and faster JRuby 1.1 with JIT support in early 2008.

* Ruby 1.9 ( development release) - The proper development release of the next ruby programming language and implementation, version, 1.9, was released in open-source form on 25th December. Not suited for production but gives the community a chance to experiment with, and provide feedback for, many new Ruby features and implementation changes before Ruby 2.0 arrives. My favorite changes are string encodings (for f.x. unicode) and the new YARV virtual machine included in the Ruby 1.9 release.

* RubyGems 1.0.0 - First proper release of RubyGems in December 2007 as open-source. With RubyGems Ruby developers neatly avoids Java’s classpath-hell (*). Basic but cool stuff!

* Active Scaffold 1.1 (almost) - I was not sure if I was going to include this plugin on the list because it is not quite ready (it’s a release candidate last updated in December) and because the old version 1.0 had a bit too many limitations for my taste. However, this open-source Rails plugin is simply too great to leave out. It’s is basically a parallel to “ActiveRecord” but for the front end allowing the developer to implement visual, model-driven CRUD operations quite neatly. You should check it out.

* Commerical IDEs with real Ruby+Rails support arrives in plenty. Proper IDE releases in 2007 includes Netbeans, Aptana (RadRails), IntelliJ, 3rdRail, Ruby In Steel, Komodo IDE etc… At last some real competition to plain old emacs or textmate !

Nb) I use this term for a lack of a more neutral yet common description of the issues with installing jars and configuring classpaths in Java. Java is a great development platform - which I have used proficiently and happily for 10+ years - but classpath/package/module/jar/version management is not it’s strong point

Ruby on Rails’ footprint on Microsoft

December 21, 2007 by Rasmus

This post could also be called how David (DHH) inspired Goliath (MS).

Lately Microsoft has release a number of products and new technologies on the .NET platform. Of course this could just be seen as the way of .NET framework. I mean.. 3.0 and 3.5 are just what comes after 2.0, right?!

Anyway… We’ve had this WebForms framework for doing websites for years now, and MS never really strafed from this strategy even though the Model View Controller pattern has had alot of followers and proven itself on the Java platform. Why should we have an MVC stack when working with .NET?!? Well.. I for one find it really easy to understand. No more advanced event cycles and s… viewstate. No more postback and advanced control design… Well that’s just me. When that is said, I’ve worked almost exclusivly with WebForms since the .NET 1.0 beta was out… Of course there has been some alternatives. In the last post I linked to the MonoRails project. On the frontpage it even says that it is inspired by Ruby on Rails (just see the .rails file extension for the httphandler in all their examples).

As I wrote in the last post, Microsoft are now launching their own MVC stack… Hmmm… Why the sudden change? As with everything Microsoft goes into, they pretty much market it as something completely new and innovative. Just look at the MVC frenzy going on over at: ScottGu and Haacked.com (ok.. also alot of great examples… ).

So is this another MS MVC post… Well not really, I just wanted to sum up some recent stuff out of MS and then leave it up to you to decide if they are not (heavily) inspired by RoR:

MVC

As already discussed, the heart of RoR and a sudden shift from MS.

REST

While not invented by DHH, he spoke (and) … and the world listened. This completely suprised MS. “Does people actually need this silly rest thingy?!”. Well apparently MS is now REST’ing a but with the Astoria Project.

NRuby?

We currently have JRuby and MS recently launched their first dynamic language (IronPython) on the .NET platform. Furthermore there are job ads like this. There is also this open source project however.

O/R Mapping

While RoR uses Martin Fowler’s Active Record pattern for its o/r mapper, Microsoft finally has something they almost can call an o/r mapper. While MS has brought the dynamic expression i c# to the table with the LINQ technology, LINQ for Entities (and SQL) is really too little too late.. I’ve been using NHibernate for years now and with the LINQ for NHibernate I might just stay there :). And also remember that this is not the first o/r mapper MS tries to bring to the market. Who doesn’t remember ObjectSpaces.

While I don’t claim that MS has picked up on o/r mappers after RoR, they certainly comes in handy with their brand new lightweight MVC package.

So…

What do you think?! Is MS inspired by RoR?! I believe they are at least keeping a very big eye on it (Sauron style) but that’s just my to cents?