Learning Ruby on Rails for a .Net MVC C# Coder

quick reference for equivalent methods/approaches

I am learning RoR for a new project that requires the ability to truly run cross-platform (don't even say Mono...). That said, I love .net, but Ruby on Rails is a fantastic MVC framework. I am learning a lot about MVC programming in general and here is my contribution to others trying to learn RoR in my situation.

And yes, I know there are many more syntax options than the ones I show, but this is just a quick reference -if you want them all, check out the API.

Everything presented here will render the same html in the respective languages, as well as behave the same as data access (ORM):

stylesheet/javascript links

Asp.Net MVC
<link rel="stylesheet" type="text/css" 
 href="<%=Url.Content("~/public/stylesheets/style.css") %>"
<script type="text/javascript" 
 src="<%=Url.Content("~/public/javascripts/jquery.js") %>"
></ script>

<%= stylesheet_link_tag 'style' %>
<%= javascript_include_tag 'jquery' %>


Asp.Net MVC
<%= Html.ActionLink("View Widgets", "Index", "Widget") %>
<%= Html.ActionLink("Widget 5", "Show", "Widget",
  new {id = 5}) %>

<%= link_to 'View Widgets', widgets_path %>
<%= link_to 'Widget 5', widget_path(5) %>


Asp.Net MVC
<% using(Html.BeginForm()) { %>
 <%= Html.LabelFor(x => x.Name) %>
 <%= Html.TextBoxFor(x => x.Name) %>
 <input type="submit" value="submit" />
<% } >

<% form_for(@widget) do |f| %>
 <%= f.label :name %>
 <%= f.text_field :name %>
 <%= f.submit 'submit' %>
<% end %>

rendering partials

Asp.Net MVC
<%= Html.Partial("form") %>

<%= render :partial => 'form' %>


Asp.Net MVC
<% foreach(var widget in Model.Widgets) { %>
  <%= widget.Name %>
<% } %>

<% for widget in @widgets %>
  <%= widget.name %>
<% end %>


Asp.Net MVC
public partial class Widget { }

public class WidgetValidation
  [Required(ErrorMessage = "Name is Required")]
  [RegularExpression("[\\S]{5,}", ErrorMessage = "Min 5 chars")]
  [StringLength(30, ErrorMessage = "Max 30 chars")]
  public string Name { get; set; }

class Widget < ActiveRecord::Base
  validates_presence_of :name
  validates_length_of :name, :in => 5..30

orm data access

Asp.Net MVC (Linq-to-Sql)
var db = new MyDataContext();
var widgets = db.Widgets;
var widget = db.Widgets.SingleOrDefault(x => x.Id == 5);
var components = widgets.Components.OrderBy(x => x.name);

RoR (ActiveRecord)
@widgets = Widget.all
@widget = Widget.find(5)
@components = @widget.components.sort_by{|w| w.name}

saving a model

Asp.Net MVC (Linq-to-Sql)
public ActionResult Create(Widget widget)

RoR (ActiveRecord)
def create
  @widget = Widget.new(params[:widget])

helper methods

Asp.Net MVC
// define it:
public static string DivThis(this HtmlHelper html, string text)
  return "<div>" + text + "</div>";

<!-- call it: -->
<= Html.DivThis("hello") %>

# define it
def div_this(text)
  '<div>' + text + '</div>'

<!-- call it: -->
<%= div_this('hello') %>

temporary data

Asp.Net MVC
// set it
TempData["someKey"] = "hello"

<!-- use it -->
<%= TempData["someKey"].ToString() %>

# set it
flash[:some_key] = 'hello'

<!-- use it -->
<%= flash[:some_key] %>

That's all I have for now, hopefully I will be adding more in the near future. I think I like RoR...

Comments (7) -

  • I wonder what makes you say "Don't even say Mono...".
  • Back when 3.5 came out, we coded up some cool stuff and tried to it running on Mono; it was a disaster.  Maybe we just didn't wait long enough or know exactly what we were doing...?
  • Such as a good tutorial and informative topic.
  • Nice article will add some new approaches in my blog. Need more examples on ASP.NET MVC!
  • well, mono is still more fashion than production ready.
    there are so many quirks one can't count, but starting from basic, they do not even have garbage collector ready yet (that releases memmory).

    one is in the work (pretty simple though) but it will take another year or so?


    when will mono be ready? 2012? NOBODY WILL CARE AT THAT TIME!!!
  • Right, which is why every gamer who has The Sims 3 on his machine has Mono installed. Mono is ready today. Keep your FUD to yourself, please.
  • Great website...and cool article man...thanx for the great post...keep on posting such articles... Resources like the one you mentioned here will be very useful to me! I will post a link to this page on my blog. I am sure my visitors will find that very useful.

Pingbacks and trackbacks (3)+

Comments are closed