Nerd Dinner on Rails
It is often said that ASP.NET MVC was inspired by Rails. What better way to test that assertion than by writing the Nerd Dinner ASP.NET MVC application in Rails? In this article, I’ll take you through the steps I used to get Nerd Dinner up and running in Rails. A few points to keep in mind:
- I had no prior experience with Rails.
- The methods employed herein are from the perspective of a “.NET guy” using his knowledge to write something in Rails.
If you happen to be a .NET guy like me and you happen to be tasked with writing or porting from a Rails environment, this article should be helpful. I implemented every major feature in Nerd Dinner (Ajax, JSON, jQuery, etc.) in what I have called Rails Dinner (a play on words re: Nerd Dinner). With that in mind, let’s get to porting Nerd Dinner to Rails!
A Quick Word About Tests
If you are familiar with a typical Rails project, tests are an integral part of the process. Just as Rails was built from the ground up with MVC in mind, unit tests are right there front and center.
Being that this is my first Rails application with the initial goal of building an application based on an existing ASP.NET MVC exemplar, I opted to set testing aside. In concept, tests in Rails are just like tests in ASP.NET MVC. For demonstration purposes, I will illustrate the mechanics of defining and running unit tests. You will see that the Rails framework provides facilities for test fixtures in order to place a database in a known good state. By default, Rails will configure a separate test environment so as not to disturb your production and development environments. Later in this article, I will discuss how Rails manages the test, development and production environments.
Testing, whether it is unit or integration testing is a first class citizen in the Rails environment. Under normal circumstances, in a TDD (Test Driven Development) scenario, you would:
- Write a test (that would initially fail)
- Write code
- Run the test/modify the code as needed until the test passes
- Repeat the process (Red, Yellow, Green Development)
When I first created the Nerd Dinner Rails project, it created a test folder. There are test fixtures and stub test classes ready to be used. You can find a nice overview of testing in Rails here: http://guides.rubyonrails.org/testing.html
If you continue to go down the Rails route, and if you are a fan of BDD (Behavior Driven Development), you will definitely want to check out Cucumber: http://github.com/aslakhellesoy/cucumber. You can find a nice overview of Cucumber here: http://www.rubyinside.com/cucumber-the-latest-in-ruby-testing-1342.html.
Cucumber provides the ability to write tests in the language of the domain. That, in a nutshell, is my mea culpa as it pertains to testing and how integral it is re: idiomatic Ruby on Rails.
Nerd Dinner, ASP.NET MVC and Rails - the Intersection
Nerd Dinner is not defined so much by its data, but rather, by the way it illustrates the ASP.NET MVC implementation. The business problem Nerd Dinner addresses is fairly trivial. There are any number of ways you can keep track of events and attendees. What is interesting about Nerd Dinner is how it illustrates, in a very simple way, the power and flexibility of the ASP.NET MVC Framework. In very short order, Nerd Dinner, in addition to the ASP.NET MVC Framework itself, illustrates how to incorporate jQuery, Ajax, JSON and forms authentication in an ASP.NET MVC application.
It is often said that ASP.NET MVC was inspired by Rails. Because I had not looked at Rails, I never understood or appreciated that point. While attending the 2009 DevLink Conference in Nashville, TN, I had an eye-opening experience. At the closing session, Richard Campbell and Carl Franklin recorded a live session of .NET Rocks! (http://www.dotnetrocks.com/default.aspx?showNum=476). The main topic of the show discussed whether software development has become too complicated.
Getting back to DevLink, during the Q and A portion of the .NET Rocks! episode, a guy named Leon Gersing came to the microphone and said, among other things, that the panel was not qualified to render it’s opinions on Ruby and Rails for the simple reason that they had not worked in that environment. I thought, “Wow, how refreshing is this?” Subsequently, Carl and Richard hosted Leon as a guest on .NET Rocks! (episode 482 - “Leon Gersing is Having a Love Affair with Ruby,” http://www.dotnetrocks.com/default.aspx?showNum=482). You can find Leon’s Web site at www.fallenrogue.com and follow him on Twitter @FallenRougue.
Leon’s comments at DevLink and his .NET Rocks! appearance inspired me to check Rails out. In the summer and fall 2009, I had already written a number of blog posts concerning Nerd Dinner. I had already integrated NHibernate/Fluent NHibernate, Structure Map and various jQuery plug-ins into the base Nerd Dinner application. With a good understanding of Nerd Dinner, I figured, why not? Why not re-write Nerd Dinner in Rails from the ground up, using the ASP.NET MVC version as the specification! How hard could it be? As it turned out, after about a week, it was not that difficult. To be candid, I didn’t develop a Rails app of high pedigree given that tests were not central to the effort. Nevertheless, the job got done!
The process had challenges. Those challenges however, were borne more out of my “noob” status re: Rails. As for Ruby, let’s just say that it is now my favorite language! With all of that in mind, I would like to take you through my journey of putting Nerd Dinner in Rails.
Getting Up and Running with Rails
Chances are you don’t have Rails installed. In order to install Rails, you must first install the Ruby language. So far, I have simply referred to Rails as Rails. Technically, what I’ll show you is Ruby on Rails. I suppose the .NET equivalent would be <<insert language here>> on ASP.NET MVC. Ruby is the language and Rails is the Web framework that provides the MVC infrastructure. Whereas in ASP, MVC was bolted on via Microsoft.Web.Mvc, Rails was built from the ground up with MVC in mind.
The best place to get up and running with Rails is to visit http://rubyonrails.org/. Once you go to the download page, you will have three tasks to complete:
- Download Ruby: The current version is 1.9.1. However, 1.9.1 has only been out for a short time. NOTE: Ruby 1.9.1. DOES NOT work with Rails version 2.3.4, which I used for this article. I wrote Rails Dinner with Ruby version 1.8.6, which has a one-click Windows installer that makes it a snap to install. You can use either version 1.8.6 or 1.8.7 of Ruby to work with this article. The Windows Installer takes care of making sure you’ve added Ruby to your path.
- Download RubyGems: RubyGems is the standard package manager for Ruby. When extending Ruby, you do so via a gem. Once you’ve downloaded RubyGems, you install it by issuing the following command at the command prompt: ruby setup.rb.
- Install Rails: This is not a misprint and there is nothing for you to download manually. Just this process alone may be enough to get you enthused about Rails. Rails is a gem you install in order to extend Ruby’s functionality. In order to install the Rails gem, or any other gem for that matter, you issue this command from the prompt: gem install rails -v=2.3.4. If you don’t specify the version, Rails 3.0, just released to beta will be installed. Rails 3.0 requires Ruby 1.8.7 or later.
If all worked correctly, you now have Ruby and Rails installed. In order to verify the versions, you can issue the commands ruby -v and rails -v from the command prompt. Figure 1 illustrates what you should see.
Figure 1: You can easily check to see which versions of Ruby and Rails you are running by using the -v switch.
For Nerd Dinner on Rails, you need to install two additional gems: the GeoKit gem and the Ruby SQLite 3 gem.
Looking at Nerd Dinner’s geography capabilities, most of that is handled via Microsoft MapPoint. If you look in the Map.ascx partial view, you will find the following script source: http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2. This facilitates the map display in Nerd Dinner. The Nerd Dinner on Rails app uses the same mapping resource. However, when it comes to calculating the distance to Dinners from search location, the original Nerd Dinner used a SQL Server stored proc that took pairs of latitude/longitude coordinates in order to provide the distance in miles. Fortunately, there is a gem called GeoKit that can handle this functionality. The following code will install these gems to your system - making them available to your Rails-based applications:gem install geokit
I’ll get into the specifics of implementing the GeoKit Gem later on.
By default, Rails supports the SQLite 3 database. Rails also supports SQL Server, MySQL, Oracle, and others. To keep this example as simple as possible, I’ll stick with SQLite 3. Before you can install the SQLite Gem, you need to install SQLite 3. You can find everything you need to install SQLite 3 here: http://www.SQLite.org/.
Once you have installed SQLite 3, you can then install the gem:gem install SQLite3-ruby
You’ll also need to use a plug-in. In the ASP.NET MVC version of Nerd Dinner, authentication is handled via ASP Forms Authentication. In order to duplicate the functionality you need to employ some method of authentication. Fortunately, there is a plug-in for that! Once you have the basic Nerd Dinner app up and running, the Restful Authentication plug-in will be installed.
By: John V. Petersen
John Petersen has been developing software for over 20 years. It all started when, as a staff accountant, he was asked to get involved in a system upgrade to replace an old IBM Series 1 computer (about the size of a large refrigerator!). Those first programs were written in Clipper, Summer 87. Since that time, John’s tools included dBase, FoxBase, Visual FoxPro and Visual Basic. An early adopter of .NET, he then decided to go to law school. After practicing law for a few years, John realized that technology was a lot more interesting than the law. Today, John focuses on ASP.NET development and is having more fun than ever solving business problems for clients. John is a Practice Director for Custom Application Development at Neudesic, a Microsoft Gold Partner and the Trusted Technology Partner in Business Innovation. A 9-time recipient of Microsoft’s Most Valuable Professional Award, John is a current ASP.NET/IIS MVP. John is also an ASP Insider and is the INETA Mentor for PA and WV. John is the author of several books and is a frequent contributor to CODE Magazine and DevPro magazine. John holds a BS in Business Administration from Mansfield University, an MBA in Information Systems from St. Joseph’s University and a JD from the Rutgers School of Law – Camden.