# Dynamically generated form + Saving multiple records at once

**URL:** <https://www.ruby-forum.com/t/dynamically-generated-form-saving-multiple-records-at-once/163526>\
**Category:** Rails\
**Created:** [March 21, 2009, 2:37pm UTC](https://www.ruby-forum.com/t/dynamically-generated-form-saving-multiple-records-at-once/163526 "2009-03-21T14:37:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcelo\_Ruiz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b782af/32.png) [@Marcelo\_Ruiz](https://www.ruby-forum.com/u/Marcelo_Ruiz)\
**Post date:** [March 21, 2009, 2:37pm UTC](https://www.ruby-forum.com/t/dynamically-generated-form-saving-multiple-records-at-once/163526/1 "2009-03-21T14:37:29Z")

</div>

Hi Everybody,

My name is Marcelo Ruiz, software developer from Argentina starting to  
work with rails.

I’m building a web app to track traffic information for roads. We have  
operators that create Reports associated to each Segment of different  
Roads.

I have the following models:

- Road (has\_many :segments)
- Segment (belongs\_to :road has\_many :reports)
- Report (belongs\_to :segment)

The requirement is that I have to show up only one form per Road, so  
the operator can load the Reports for all the Segments in that Road at  
once.

This would be something similar to this:

## Road “Route 55”

Report for Segment 1: (text field)

Report for Segment 2: (text field)

Report for Segment 3: (text field)

(SUBMIT BUTTON)

This means that I will have to:

1. Dinamically generate the fields, according to the number of  
segments the selected Road has.
2. Create multiple Reports at the same time.

Any advise on how to do this?

---

<div class="post-metadata">

**Author:** ![Marcelo\_Ruiz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b782af/32.png) [@Marcelo\_Ruiz](https://www.ruby-forum.com/u/Marcelo_Ruiz)\
**Post date:** [March 21, 2009, 2:56pm UTC](https://www.ruby-forum.com/t/dynamically-generated-form-saving-multiple-records-at-once/163526/2 "2009-03-21T14:56:16Z")

</div>

I think you will be happy to watch this screencast

> **[\#75 Complex Forms Part 3 - RailsCasts](http://railscasts.com/episodes/75-complex-forms-part-3)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

Its a project that has\_many tasks but it talks about exactly the  
problem you have with build\_segments 3.times…

---

<div class="post-metadata">

**Author:** ![Marcelo\_Ruiz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b782af/32.png) [@Marcelo\_Ruiz](https://www.ruby-forum.com/u/Marcelo_Ruiz)\
**Post date:** [March 23, 2009, 9:43pm UTC](https://www.ruby-forum.com/t/dynamically-generated-form-saving-multiple-records-at-once/163526/3 "2009-03-23T21:43:09Z")

</div>

Thank you Freddy. That wasn’t exactly what I needed to do, but the  
screencasts helped me in another case.

What I actually need to do is simpler: create multiple Reports at the  
same time. Each report has a description with a textfield and I have  
to associate it with a different segment. That’s all.

Any help? Thanks!

---

<div class="post-metadata">

**Author:** ![Marcelo\_Ruiz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b782af/32.png) [@Marcelo\_Ruiz](https://www.ruby-forum.com/u/Marcelo_Ruiz)\
**Post date:** [March 23, 2009, 10:09pm UTC](https://www.ruby-forum.com/t/dynamically-generated-form-saving-multiple-records-at-once/163526/4 "2009-03-23T22:09:35Z")

</div>

This is a code sample of what I have made so far:

# New Report for \<%= @way.road.name%\>, \<%= @way.name%\>

\<%= form\_tag :action =\> :create, :id =\> @way.id%\>

```
<p>Date: <%= datetime_select("report", "time") %> </p>

<p>Priority:
<%= radio_button("report", "priority", 1) %> high -
<%= radio_button("report", "priority", 2, {:checked => "checked"})

```

%\> medium -  
\<%= radio\_button(“report”, “priority”, 3) %\> low

```
<% for segment in @segments%>

    <%= hidden_field("report[]", "segment_id", { :value =>

```

segment.id}) %\>

```
    <p>Segment: <strong><%= segment.name%></strong></p>

    <p>Traffic Status:
    <%= select("report[]", "status_id", Status.find(:all, :order

```

=\> “Severity”).map {|u| [u.name, u.id]}) %\>

```
    <p>Description:<br/>
    <%= text_area("report[]", "description", {:rows => 6, :cols =>

```

70}) %\>

```
    <p>Short Desc.:<br/>
    <%= text_area("report[]", "short_description", {:rows =>

```

2, :cols =\> 70}) %\>

```
<% end %>

<p><%= submit_tag %></p>

```

This form renders well. Notice how I have some fields of Report that  
are common to all segment reports (Hour and Priority) and some that  
vary (segment\_id, status\_id, description and short\_description). I’m  
prefixing the first fields with “report” and the others with “report  
[]”. I’m not sure if it’s a good idea.

Now I don’t have any idea how I should handle this in the controller.

Any idea?

Thanks!
