.NET developer trying to understand some Rails basics

Hi All,
I’ve been a ASP.NET developer for the last few years and consider it to
be a pretty productive environment to work with. However, the
object-relational mapping (ActiveRecord) and simplicity of the Rails
framework and Ruby in general really appeals to me. .NET currently
doesn’t have something like Rails’ ORM - atleast not out-of-the-box.

Here’s my question:
In asp.net I am able to use declaritive syntax like the following to
implement very complex UI elements.

<asp:GridView ID=“GridView1” AllowSorting=“true” AllowPaging=“true”
Runat=“server”
DataSourceID=“SqlDataSource1” AutoGenerateEditButton=“true”
DataKeyNames=“au_id”
AutoGenerateColumns=“False”>

<asp:BoundField ReadOnly=“true” HeaderText=“ID”
DataField=“au_id” SortExpression=“au_id” />
<asp:BoundField HeaderText=“Last Name” DataField=“au_lname”
SortExpression=“au_lname” />
<asp:BoundField HeaderText=“First Name” DataField=“au_fname”
SortExpression=“au_fname” />
<asp:BoundField HeaderText=“Address” DataField=“address”
SortExpression=“address” />
<asp:BoundField HeaderText=“City” DataField=“city”
SortExpression=“city” />
<asp:BoundField HeaderText=“Zip Code” DataField=“zip”
SortExpression=“zip” />
<asp:CheckBoxField HeaderText=“Contract”
SortExpression=“contract” DataField=“contract” />

</asp:GridView>

This simple bit of code creates a nice table/grid with already built-in
paging, sorting, as well as buttons which will automatically make a row
editable. In order to tie this to actual data, all I have to do is
create a DataSource object with the name of the
DataSourceID=“SqlDataSource1”.

Of course, this is just one of the many “Server Controls” as they are
called. I’m wondering if rails has functionality like this or how hard
it is to create my own reusable ‘controls’ like this. I would especially
be interested in hearing from any others that have done asp.net
development in the past.

Thanks in advance.

You’re going to have to do a little bit of work on your end depending
on the functionality you’re looking for. In Rails there are several
items that can be used to do what you are looking for. There’s
probably more, but here’s what I have off the top of my head:

  1. Helpers - Built into rails are a series of helpers, like
    date_select. Others have implemented addon helpers, like a calendar
    popup helper. Check out this page:
    http://wiki.rubyonrails.org/rails/pages/3rdPartyHelpers/versions/60

  2. Plugins - Read about plugins on the Wiki site.
    http://wiki.rubyonrails.org/rails/pages/Plugins. Plugins are less
    focussed on UI and more on functionality, like act_as_authenticated.
    This is a generalization but you get the idea.

  3. Widgets - This is an interesting idea an one that would work for
    some of these types of items you want (http://blogrium.com/?p=60).

  4. Components - I note these here only for completeness, but current
    thinking says use them as a last resort. Components are like Widgets,
    only with a full controller. In the past, like a month ago, there
    were real performance issues with components.

  5. Ajax Toolkits - If you’re looking to do ajax type stuff there’s
    lots of toolkits out there that will provide a very .net type of
    functionality. It may not be as cut and paste, but for the most part
    is pretty straightfoward. I would get familiarized with scriptaculous
    / prototype first, because it’s very well integrated with Rails (look
    into RJS). There’s others out there too, like the Yahoo UI, Dojo,
    Rico, Zimbra, etc… There’s something like 150 of them now and each
    have their strengths.

Michael

Would you mind outlining the approach you took to create such a
table/grid?

-Jonathan.

Hi Nate,

I was an ASP.NET developer a couple of years ago until I gave Rails a
try and realized it was an order of magnitude better. You can have
the same functionality in Rails that you described with the server
control but in Rails you won’t be using ‘controls’ per se. When it
comes to the forms you don’t use controls in the ASP.NET sense with
Rails, you use even simpler Rails tags. Without going into the
details, I can tell you that from DB to Browser I can write much
simpler code to create a table/grid of data, with paging, and sorting
as well and editable rows…

It’s just in Rails I’d go about it differently than ASP.NET

This isn’t a grid, if I wanted a grid I would lay things out in
Tables, but a problem with using ASP.NET controls such as an ASP.NET
datagrid is that it implies a grid is a good way to display data to a
user which it may or may not be but with Rails you can take either
approach. The code that follows will show a list items, allow you to
page and sort and also add new items. (Also this is not the simplest
possible answer but the one that I can provide quickly)

**SORT:

Sort by: <%= link_to_unless @sort == "alphabetically", "Alpha", :action => "show_view_all_items", :id => @collection.id, :sort => "alphabetically" %> | <%= link_to_unless @sort == "date", "Date", :action => "show_view_all_items", :id => @collection.id, :sort => "date" %> | <%= link_to_unless @sort == "wows", "Wows!", :action => "show_view_all_items", :id => @collection.id, :sort => "wows" %>

**SHOW LIST OF ITEMS:

<%= render :partial => “item”, :collection => @items %>

**AJAX SHOW HIDDEN FORM AND SUBMIT WITHOUT POSTBACK, UPDATING LIST OF
ITEMS

Create a new Item

<%= form_remote_tag(:url => {:action => 'create'}, :update => "items", :position => :top, :loading => 'item_loading()', :success => 'item_added()', :failure => 'item_edit()') %> <%= render :partial => 'form' %> <%= submit_tag "Add item", :id => "add_item_button" %> <%= image_tag "/images/ajax-loader.gif" %> close <%= end_form_tag %>
<% end %>

**PAGING

<%= link_to ‘Previous page’, { :page => @item_pages.current.previous }
if @item_pages.current.previous %>
<%= link_to ‘Next page’, { :page => @item_pages.current.next } if
@item_pages.current.next %>

the application_helper.rb is available to all views. You can also tell
your controller that you want to use a specific helper with the helper
:helper_name method. Then when you reference it in the view it finds
it.

Michael

**SHOW LIST OF ITEMS:

<%= render :partial => “item”, :collection => @items %>

Hi Tim,
I’m wondering what your partial shown above might look like. Also, how
is the syntax you use there different from using
render_partial_collection?

Thanks.

Michael T. wrote:

You’re going to have to do a little bit of work on your end depending
on the functionality you’re looking for. In Rails there are several
items that can be used to do what you are looking for. There’s
probably more, but here’s what I have off the top of my head:

  1. Helpers - Built into rails are a series of helpers, like
    date_select. Others have implemented addon helpers, like a calendar
    popup helper. Check out this page:
    http://wiki.rubyonrails.org/rails/pages/3rdPartyHelpers/versions/60

Michael

Thanks Michael for your list. Very helpful. Speaking of help:

From the docs on the Wiki regarding helpers, it looks like a helper is
only available in the ‘associated view’. Is there any way to create a
helper (or something very similar to a helper) that can be available
more widely?

Thanks.

Hi,

Glad to hear you are having a good result with RoR.

I’m from ASP.Net [VB.Net, C#, Visual Studio and SQL Server].

I’m planning on putting 3 apps on RoR [I’m meeting my first Customer in NYC to plan out the app and the algorithm - he is a math whiz and we’re planning on putting up a QA System].

My colleagues think I’m nuts but I see something in RoR that I never saw
in ASP.Net or Java. My ASP.Net library is about a yard wide and so is my
Java library. With Ror, I’m using four books.

I think I’ve made the right move – wish me luck.

Cheers,
Pat

Nate Davis wrote:

<asp:GridView ID=“GridView1” AllowSorting=“true” AllowPaging=“true”
Runat=“server”
DataSourceID=“SqlDataSource1” AutoGenerateEditButton=“true”
DataKeyNames=“au_id”

Hi Nate,

Welcome to the Rails community! Going from .Net to Rails has involved
many moments of mind-shifting for me; usually those moments are along
the lines of, “you mean that’s all I have to do? That’s it? Really?..”
:slight_smile:

For a grid, you might start with a plugin we developed to do something
similar to an ASP.NET gridview:

http://www.softiesonrails.com/articles/2006/05/02/the-datalist_for-plugin

Now, it’s not going to have all the bells and whistles of the server
control, but that’s because given all the easy things you can do in
Rails (like paging), we kept our plug in really simple. Check it out
and let me know if you need any help with it.

Jeff

On 5/26/06, Jeff C. [email protected] wrote:

Nate Davis wrote:

<asp:GridView ID=“GridView1” AllowSorting=“true” AllowPaging=“true”
Runat=“server”
DataSourceID=“SqlDataSource1” AutoGenerateEditButton=“true”
DataKeyNames=“au_id”

A different approach to get something like this up and running and
rails is a code generator. In this case, something like
http://www.ajaxscaffold.com/ might be a good fit.

Jeff C. wrote:

Nate Davis wrote:

<asp:GridView ID=“GridView1” AllowSorting=“true” AllowPaging=“true”
Runat=“server”
DataSourceID=“SqlDataSource1” AutoGenerateEditButton=“true”
DataKeyNames=“au_id”

Hi Nate,

Welcome to the Rails community! Going from .Net to Rails has involved
many moments of mind-shifting for me; usually those moments are along
the lines of, “you mean that’s all I have to do? That’s it? Really?..”
:slight_smile:

Jeff
softiesonrails.com

Thanks for the input. As much as I like what Ruby and Rails has to
offer, I really couldn’t just say - “Well, I’m moving over to Rails”.
Ruby/Rails has many advantages over .NET and .NET has many advantages
over Ruby/Rails. It’s best for my career (and any developer’s career) to
gain expertise in atleast two very different development environments.
By keeping up with both .NET and Ruby/Rails, I can be ready to tackle
any project. I can see that some projects will be best accomplished with
.NET and others with Rails.

Also, I think learning Rails is actually making me a better .NET
programmer, because I’m thinking in new ways - trying to use .NET in the
Rails way (if that makes sense).

render_partial_collection is deprecated and may not be supported in the
future.

Nate wrote:

**SHOW LIST OF ITEMS:

<%= render :partial => “item”, :collection => @items %>

Hi Tim,
I’m wondering what your partial shown above might look like. Also, how
is the syntax you use there different from using
render_partial_collection?

Thanks.

Nate Davis wrote:

Thanks for the input. As much as I like what Ruby and Rails has to
offer, I really couldn’t just say - “Well, I’m moving over to Rails”.

Right - didn’t mean to imply that if I did. I’m a C# developer by day,
and I use Rails at night for my other (unpaid) job. :slight_smile:

Also, I think learning Rails is actually making me a better .NET
programmer, because I’m thinking in new ways - trying to use .NET in the
Rails way (if that makes sense).

That’s a great point. I hadn’t actually learned a new language in a
while, and Ruby has been a fun challenge for me. My only exposure to
other scripting languages were not strongly typed and not object
oriented, so to me Ruby is just amazing.

The more I’m able to think “in Ruby”, often the more I see how to write
better C# code as well. One thing I’ve learned from the Ruby community
here is really how useful - and almost necessary - it is to be familiar
with more than just one programming language. I had been doing C++/C#
for so long I wasn’t getting the benefits of the mental training that
multilingual developers get every day.

Jeff