Any tutorials on how to do multiple row (data grid like) form?


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

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

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|

or however you want to handle that.

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

Hope that helps!

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



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?