Forum: Ruby on Rails .NET developer trying to understand some Rails basics

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Nate Davis (Guest)
on 2006-05-25 06:09
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">
        <Columns>
          <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" />
        </Columns>
      </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.
Michael T. (Guest)
on 2006-05-25 07:03
(Received via mailing list)
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/3rdPartyHe...

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
Tim C. (Guest)
on 2006-05-25 07:47
(Received via mailing list)
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...
Jonathan V. (Guest)
on 2006-05-25 07:59
(Received via mailing list)
Would you mind outlining the approach you took to create such a
table/grid?

-Jonathan.
Tim C. (Guest)
on 2006-05-25 08:30
(Received via mailing list)
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:

<span class="sorttype">
	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" %>
</span>


**SHOW LIST OF ITEMS:

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


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

<p class="newitem_container"><a href="#additem"
onclick="Effect.toggle('additem','blind'); return false"
class="newitem">Create a new Item</a></p>
<div id="additem" class="showhidedialog dialogbg" style="display:
none;">
<%= 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" %>
	<span id="busy" style ="display: none;"><%= image_tag
"/images/ajax-loader.gif" %></span><a href="#"
onclick="Effect.toggle('additem','blind'); return false"> close</a>
<%= end_form_tag %>
</div>
<% 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 %>
Nate (Guest)
on 2006-05-25 17:26
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/3rdPartyHe...
>
> .....
>
> 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.
Michael T. (Guest)
on 2006-05-25 17:54
(Received via mailing list)
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
Nate (Guest)
on 2006-05-25 21:07
>
> **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.
cyril (Guest)
on 2006-05-25 21:19
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.
Pat L. (Guest)
on 2006-05-26 03:19
(Received via mailing list)
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
Jeff C. (Guest)
on 2006-05-26 08:17
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?..."
:-)

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

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
softiesonrails.com
Jonathan Flynn (Guest)
on 2006-05-26 08:42
(Received via mailing list)
On 5/26/06, Jeff C. <removed_email_address@domain.invalid> 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.
Nate D. (Guest)
on 2006-05-26 16:40
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?..."
> :-)
>
> ........
>
> 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).
Jeff C. (Guest)
on 2006-05-26 18:21
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. :-)

> 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
This topic is locked and can not be replied to.