# Any tutorials on how to do multiple row (data grid like) for

**URL:** <https://www.ruby-forum.com/t/any-tutorials-on-how-to-do-multiple-row-data-grid-like-for/106177>\
**Category:** Rails\
**Created:** [June 7, 2007, 4:07am UTC](https://www.ruby-forum.com/t/any-tutorials-on-how-to-do-multiple-row-data-grid-like-for/106177 "2007-06-07T04:07:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Johnston](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@Chris\_Johnston](https://www.ruby-forum.com/u/Chris_Johnston)\
**Post date:** [June 7, 2007, 4:07am UTC](https://www.ruby-forum.com/t/any-tutorials-on-how-to-do-multiple-row-data-grid-like-for/106177/1 "2007-06-07T04:07:38Z")

</div>

Hello,

I am trying to create a multiple row / data grid like input form and am  
not  
sure how to proceed. I am wondering if there are any good tutorials on  
how  
to do something like this.

To be more exact in what I am trying to do, imagine it is an expense  
report  
application in which a user has to enter the expense amount, a  
description,  
and select the category under which the expense belongs. The form  
consists  
of 5 rows, each row consisting of a field to enter each piece of  
information  
for a single expense. The form would also offer the user the chance to  
add  
or subject rows.

I am looking for a tutorial that covers both the view and controller  
side of  
doing this as I am fairly new to Rails.

Thank you for you help with this.

Chris Johnston

–  
[www.fuzzylizard.com](http://www.fuzzylizard.com)

You know you’ve achieved perfection in design, Not when you have nothing  
more to add, But when you have nothing more to take away.  
– Antoine de Saint-Exupery

---

<div class="post-metadata">

**Author:** ![Chris\_Johnston](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@Chris\_Johnston](https://www.ruby-forum.com/u/Chris_Johnston)\
**Post date:** [June 7, 2007, 5:20am UTC](https://www.ruby-forum.com/t/any-tutorials-on-how-to-do-multiple-row-data-grid-like-for/106177/2 "2007-06-07T05:20:04Z")

</div>

I think you may be looking for something like this:

\<% form\_for :expenses do |form| %\>  
\<% @expenses.each\_with\_index do |expense, index| %\>  
\<%= form.text\_field(:amount, :index =\> index) %\>  
\<%= form.text\_field(:description, :index =\> index) %\>  
\<% end %\>  
\<% submit\_tag %\>  
\<% end %\>

then in the controller you should be able to access the data as a hash  
of hashes:

params[:expenses].keys.each do |index|  
Expense.create(params[:expenses][index])  
end

or however you want to handle that.

None of that is tested but I \>think\< that is the general idea.

Hope that helps!  
Tim

---

<div class="post-metadata">

**Author:** ![Chris\_Johnston](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@Chris\_Johnston](https://www.ruby-forum.com/u/Chris_Johnston)\
**Post date:** [June 7, 2007, 6:07am UTC](https://www.ruby-forum.com/t/any-tutorials-on-how-to-do-multiple-row-data-grid-like-for/106177/3 "2007-06-07T06:07:08Z")

</div>

Cool, from reading through it, it looks like might just do it. Thanks.

Chris

On 6/6/07, tmac [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> \<% end %\>  
> None of that is tested but I \>think\< that is the general idea.  
> how
> 
> > for a single expense. The form would also offer the user the chance to
> > 
> > –www.fuzzylizard.com
> > 
> > You know you’ve achieved perfection in design, Not when you have nothing  
> > more to add, But when you have nothing more to take away.
> > 
> > - Antoine de Saint-Exupery
> 
> >

–  
[www.fuzzylizard.com](http://www.fuzzylizard.com)

You know you’ve achieved perfection in design, Not when you have nothing  
more to add, But when you have nothing more to take away.  
– Antoine de Saint-Exupery

---

<div class="post-metadata">

**Author:** ![Chris\_Johnston](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@Chris\_Johnston](https://www.ruby-forum.com/u/Chris_Johnston)\
**Post date:** [June 9, 2007, 5:08am UTC](https://www.ruby-forum.com/t/any-tutorials-on-how-to-do-multiple-row-data-grid-like-for/106177/4 "2007-06-09T05:08:02Z")

</div>

Hi,

It isn’t working for me. Are there any good tutorials on putting  
together  
complex forms?

What I have is this:

Object A, in my example it is ExpenseReport. ExpenseReport contains  
several  
fields of its own like submitted\_date, current\_project, comments.

It also has a collection of LineItems which contains the amount,  
establishment, comments.

A LineItem contains an ExpenseType which is another model object. In the  
form, this would be represented by a drop down box. So the whole form  
looks  
something like this:

\<% form\_for :report, :url =\> { :action =\> ‘create’} do |form| %\>

```
  <label for="report_project">Name</label>
  <%= text_field 'report', :project, :size => 40 %>

  <label for="report_submit_date">Report Submitted on</label>
  <%= datetime_select 'report', 'submit_date' %>

    Report Line Item</legend>

      <% fields_for :line_items do |line_item_form| %>
        <% @line_items.each_with_index do |line_item, index| %>

          <% fields_for :expense_categories do |expense_category| %>
            <%= expense_category.collection_select(:id,

```

@expense\_category, :id, :name) %\>  
\<% end %\>

```
          <%= text_field :line_item_form, :amount, :index => index 

```

%\>  
\<% end %\>  
\<% end %\>

```
  <label for="report_comments">Comments</label>
  <%= text_area 'report', 'comments', :rows => 5, :cols => 60 %>
  <%= submit_tag "Create" %>

<% end %>

```

The problem that I am having right now is twofold; first, when I use the  
code listed below to extract the line\_items and create those objects I  
am  
getting a ‘nil object’ error and second, in the params, there is only  
one  
listing for the expense\_category instead of 5.

Anyone have ideas or tutorials they can point me to for this kind of  
thing?

Thanks,  
Chris

On 6/6/07, tmac [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> \<% end %\>  
> None of that is tested but I \>think\< that is the general idea.  
> how
> 
> > for a single expense. The form would also offer the user the chance to
> > 
> > –www.fuzzylizard.com
> > 
> > You know you’ve achieved perfection in design, Not when you have nothing  
> > more to add, But when you have nothing more to take away.
> > 
> > - Antoine de Saint-Exupery
> 
> >

–  
[www.fuzzylizard.com](http://www.fuzzylizard.com)

You know you’ve achieved perfection in design, Not when you have nothing  
more to add, But when you have nothing more to take away.  
– Antoine de Saint-Exupery
