loads of useful information, examples and tutorials pertaining to web development utilizing asp.net, c#, vb, css, xhtml, javascript, sql, xml, ajax and everything else...

 



Advertise Here
C-Sharpener.com - Programming is Easy!  Learn Asp.Net & C# in just days, Guaranteed!

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

by naspinski 9/15/2010 12:07:00 PM

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>

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


hyperlinks

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

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


forms

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

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


rendering partials

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

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


looping

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

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


validation

Asp.Net MVC
[MetadataType(typeof(WidgetValidation))]
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; }
}

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


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)
{
  db.InsertOnSubmit(widget);
  db.SubmitChanges();
}

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


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") %>

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

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



temporary data

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

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

RoR
# 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...

Tags: , , ,

c# | mvc

Related posts

Comments

9/17/2010 2:10:12 PM

Dave Van den Eynde
I wonder what makes you say "Don't even say Mono...".

Dave Van den Eynde be

9/17/2010 2:12:27 PM

naspinski
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...?

naspinski us

9/18/2010 12:21:36 AM

pingback
Pingback from topsy.com

Twitter Trackbacks for

Learning Ruby on Rails for a .Net MVC C# Coder
[naspinski.net]
on Topsy.com

topsy.com

9/19/2010 4:20:09 PM

arts college
Such as a good tutorial and informative topic.

arts college au

9/20/2010 7:42:30 AM

Agafonov
Nice article will add some new approaches in my blog. Need more examples on ASP.NET MVC!

Agafonov ua

9/23/2010 4:46:19 PM

uzivatel.tri
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?

COMEON

when will mono be ready? 2012? NOBODY WILL CARE AT THAT TIME!!!

uzivatel.tri

9/24/2010 5:06:27 AM

Dave Van den Eynde
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.

Dave Van den Eynde be

10/1/2010 1:05:34 PM

Grohe
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.

Grohe us


Comments are closed