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