idisposable.net: a blog about web 2.0, search, collaboration, Ruby on Rails, Microsoft, Google, and other fun stuff

Converting from ASP.NET to Rails: Part 2

This is part 2 in a series of articles on converting both your mindset and your ASP.NET web sites to Rails. Inside I hope to help anyone coming from a .NET background that is looking to create new Rails apps, or migrating existing ones.

Before we go forward, I assume that you have a basic understanding of Ruby and Rails. If not, read _why’s poignant guide to Ruby and buy Agile Development with Rails and run through at least the first couple of chapters.

One of the biggest challenges I had initially when starting with Rails was understanding the relationships between the web framework concepts I knew well from ASP.NET, and these new Rails concepts.

I discovered these key concepts from ASP.NET translate well into Rails:

  • Master Pages
  • User Web Controls
  • The ASP.NET Web Control / PostBack Event Model
  • The use and arrangement of .aspx, .ascx, and code-behind (.cs or .vb)
ASP.NET Concept Rails Concept
Master Pages Layouts
User Web Controls Partials
The “glue”:ASP.NET Web Controls.aspx,.ascx, and code behind (.cs or .vb) Rails MVCpage.rhtml _partial.rthml, and model.rb / controller.rb / helper.rb

Table 1: ASP.NET Concepts and their Rails counterparts

These concepts are not exact, as these are two completely different frameworks. Let’s take a look at each of these concepts in more detail.

Master Pages and Layouts

A Master Page gives you a canvas from which you can build other pages on without having to worry about all of the common elements like headers, footers, HTML directives, etc. Rails layouts are designed for a similar purpose and as such translate very well to ASP.NET Master Pages. As you will see in the next chapter in this series, I was able to convert an ASP.NET Master Page (site.master) to a Rails layout (application.rhtml) very quickly. ASP.NET Master Pages allow for multiple discreet content replacement zones, called ContentPlaceHolders. Rails does not have a preset “tag” for zones, rather you can use variables and a single “yield” statement to render discreet sections. It is best to reserve the “yield” for the main content body, and use variables for supplementary content like headers and titles that might be replaced.

This ASP.NET Master Page:


<@Page Title="Page Title">

Lorem ipsum blah blah blah

<asp:ContentPlaceHolder id="headerContentPlaceHolder" runat="server"></asp:ContentPlaceHolder>

Ipsum lorem blah blah blah

<asp:ContentPlaceHolder id="bodyContentPlaceHolder" runat="server"></asp:ContentPlaceHolder>

is functionally equivalent to this Rails layout:


<title><%=@title || "Default Title"%></title>
Ipsum lorem blah blah blah

<%=@header_content %>

Ipsum lorem blah blah blah

<%=yield%>

User Web Controls and Partials

In ASP.NET, User Web Controls (.ascx files), give you a nice way to encapsulate common web presentation elements. They can be included on any page or template, and have their own event model that custom code can be added to, much like ASP.NET Pages. An excellent example of a User Web Control would be a “contact us” widget that allows a user to fill out and submit a contact form in a consistent way across many pages of a website.

Rails partials serve a similar purpose. They allow a developer to encapsulate HTML with the intent of reusing across many pages or layouts. The “contact us” widget example above also fits well with partials.

From the excellent overview at the Rails Diary:

Partials let you break out a chunk of RHTML that is going to be used across multiple views in a controller or even across multiple controllers.

For more information, partials API Documentation can be found here.

In the next part of the series we’ll wrap up the ASP.NET concept comparison and start to look at implementing real-world ASP.NET to Rails conversion.

One Comment

  1. Posted September 25, 2007 at 5:15 pm | Permalink

    great article! Rails Rocks!

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*