# Repeating form

**URL:** <https://www.ruby-forum.com/t/repeating-form/134335>\
**Category:** Rails\
**Created:** [April 9, 2008, 10:33pm UTC](https://www.ruby-forum.com/t/repeating-form/134335 "2008-04-09T22:33:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![martinevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@martinevans](https://www.ruby-forum.com/u/martinevans)\
**Post date:** [April 9, 2008, 10:33pm UTC](https://www.ruby-forum.com/t/repeating-form/134335/1 "2008-04-09T22:33:13Z")

</div>

I have a controller that creates a new sale  
def new  
@bakery\_output = BakeryOutput.new  
end

# GET /bakery\_outputs/1;edit

def edit  
@bakery\_output = BakeryOutput.find(params[:id], :include =\>  
[:recipe, :customer])  
end

# POST /bakery\_outputs

# POST /bakery\_outputs.xml

def create  
@bakery\_output = BakeryOutput.new(params[:bakery\_output])

```
respond_to do |format|
  if @bakery_output.save
    flash[:notice] = 'Sale was successfully created.'
    format.html { redirect_to bakery_output_url(@bakery_output) }
    format.xml { head :created, :location =>

```

bakery\_output\_url(@bakery\_output) }  
else  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @bakery\_output.errors.to\_xml }  
end  
end  
end

and a view to go with it

| 
### New Sale\<%= error\_messages\_for :bakery\_output %\> \<% form\_for(:bakery\_output, :url =\> bakery\_outputs\_path) do |f| %\>
 |
| &nbsp; |
| **Customer** | **Recipe** | **Qty** | **Date** | **Batchcode** |
| \<% @customers = Customer.find(:all, :order =\> "company")%\> \<%= f.collection\_select(:customer\_id, @customers, :id, :company) %\> | \<% @recipes = Recipe.find(:all, :order =\> "name")%\> \<%= f.collection\_select(:recipe\_id, @recipes, :id, :name) %\> | \<%= f.text\_field :sales\_quantity, :size =\> 3 %\> | \<%= f.date\_select :saledate %\> | \<%= f.text\_field :salebatchcode, :size =\> 10 %\> |
| \<%= submit\_tag "Create" %\> |
| \<% end %\> \<%= link\_to 'Cancel', bakery\_outputs\_path %\> |

I want to be able to initiially have the ability to have 5 sales, saved  
to the database, and after that to make it ajax friendly using partials  
etc. I want to do this 1 stage at a time as I am new to rails and want  
to try to understand more fully what is going on. So my question is how  
do I achieve the 5 sales from a display and controller point of view.  
thanks  
Martin

---

<div class="post-metadata">

**Author:** ![martinevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@martinevans](https://www.ruby-forum.com/u/martinevans)\
**Post date:** [April 9, 2008, 10:46pm UTC](https://www.ruby-forum.com/t/repeating-form/134335/2 "2008-04-09T22:46:32Z")

</div>

Martin E. wrote:

> I want to be able to initiially have the ability to have 5 sales, saved  
> to the database, and after that to make it ajax friendly using partials  
> etc. I want to do this 1 stage at a time as I am new to rails and want  
> to try to understand more fully what is going on. So my question is how  
> do I achieve the 5 sales from a display and controller point of view.  
> thanks

Not sure exactly if this is what you want but it sounds similar and may  
be the starting point you need:

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

---

<div class="post-metadata">

**Author:** ![martinevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@martinevans](https://www.ruby-forum.com/u/martinevans)\
**Post date:** [April 9, 2008, 10:59pm UTC](https://www.ruby-forum.com/t/repeating-form/134335/3 "2008-04-09T22:59:54Z")

</div>

Mark B. wrote:

> Martin E. wrote:
> 
> > I want to be able to initiially have the ability to have 5 sales, saved  
> > to the database, and after that to make it ajax friendly using partials  
> > etc. I want to do this 1 stage at a time as I am new to rails and want  
> > to try to understand more fully what is going on. So my question is how  
> > do I achieve the 5 sales from a display and controller point of view.  
> > thanks
> 
> Not sure exactly if this is what you want but it sounds similar and may  
> be the starting point you need:
> 
> [#74 Complex Forms Part 2 - RailsCasts](http://railscasts.com/episodes/74)

Yes thanks I’ve seen that and could see how it worked where there was a  
one to many relationship between project and tasks. I tried implimenting  
some of the ideas and failed miserably. It’s the initial part I need  
most assistance with I think after that I would be able to apply the  
knowledge from the screencasts.  
thanks  
Martin

---

<div class="post-metadata">

**Author:** ![martinevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@martinevans](https://www.ruby-forum.com/u/martinevans)\
**Post date:** [April 10, 2008, 1:32am UTC](https://www.ruby-forum.com/t/repeating-form/134335/4 "2008-04-10T01:32:32Z")

</div>

Ah okay.

So in your controller, in the new action, add in @sales = [Sale.new] \* 5

In the view, loop over them like this:

\<% for sale in @sales %\>  
Product: \<%= text\_field ‘sale[]’, ‘product’ %\>  
Amount: \<%= text\_field ‘sale[]’, ‘amount’ %\>  
\<% end %\>

I’m guessing you’ve got product and amount… and you want them stored  
as text… you could have anything… the important thing is the []  
specifier

Then in your receiving controller action (create), you need to loop  
through, updating them all using a special syntax

Sale.update(params[:sale].keys, params[:sale].values)

Julian.

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)  
VIDEO #3 out NOW!  
[http://sensei.zenunit.com/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![martinevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@martinevans](https://www.ruby-forum.com/u/martinevans)\
**Post date:** [April 10, 2008, 1:26am UTC](https://www.ruby-forum.com/t/repeating-form/134335/5 "2008-04-10T01:26:07Z")

</div>

Hi,

On 10/04/2008, at 6:59 AM, Martin E. wrote:

> Yes thanks I’ve seen that and could see how it worked where there  
> was a  
> one to many relationship between project and tasks. I tried  
> implimenting  
> some of the ideas and failed miserably. It’s the initial part I need  
> most assistance with I think after that I would be able to apply the  
> knowledge from the screencasts.  
> thanks  
> Martin

These are the steps:

Create a rails project  
Configure your database  
Create a controller called sales. Use script/generate controller sales  
Create an action in the sales controller called index  
Create a model called Sale. Use script/generate model Sale  
Fill in the migration (it’s in db/migrations folder) to include at  
least an attribute called name (for our demo)  
in the index action, add the following line:  
@sales = Sale.find(:all)  
now, in your views volder, go into the sales subfolder, and create a  
file called index.rhtml  
in that file, add the following:

\<% for sale in @sales %\>

Name: \<%= sale.name %\>

\<% end %\>

this will loop through all the sales, printing out their name.

If you need help with any of those steps let us know.

Now, what model do you want to assocation to sales? Then we can go  
from there.

Julian.

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)  
VIDEO #3 out NOW!  
[http://sensei.zenunit.com/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![martinevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@martinevans](https://www.ruby-forum.com/u/martinevans)\
**Post date:** [April 10, 2008, 10:17pm UTC](https://www.ruby-forum.com/t/repeating-form/134335/6 "2008-04-10T22:17:15Z")

</div>

Julian L. wrote:

> Ah okay.
> 
> So in your controller, in the new action, add in @sales = [Sale.new] \* 5
> 
> In the view, loop over them like this:
> 
> \<% for sale in @sales %\>  
> Product: \<%= text\_field ‘sale[]’, ‘product’ %\>  
> Amount: \<%= text\_field ‘sale[]’, ‘amount’ %\>  
> \<% end %\>
> 
> I’m guessing you’ve got product and amount… and you want them stored  
> as text… you could have anything… the important thing is the []  
> specifier
> 
> Then in your receiving controller action (create), you need to loop  
> through, updating them all using a special syntax
> 
> Sale.update(params[:sale].keys, params[:sale].values)
> 
> Julian.
> 
> Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)  
> VIDEO #3 out NOW!  
> [http://sensei.zenunit.com/](http://sensei.zenunit.com/)  
> Thanks for your help I think that will assist me a lot, but what do I  
> use for the form notation

regards  
Martin

---

<div class="post-metadata">

**Author:** ![martinevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@martinevans](https://www.ruby-forum.com/u/martinevans)\
**Post date:** [April 12, 2008, 9:30pm UTC](https://www.ruby-forum.com/t/repeating-form/134335/7 "2008-04-12T21:30:32Z")

</div>

Julian L. wrote:

> Just what you’d usually use.
> 
> Julian.
> 
> Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)  
> VIDEO #3 out NOW!  
> [http://sensei.zenunit.com/](http://sensei.zenunit.com/)  
> Hi I’m still struggling with this  
> what should my form\_for line say

thanks for any help given  
regards  
Martin

---

<div class="post-metadata">

**Author:** ![martinevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@martinevans](https://www.ruby-forum.com/u/martinevans)\
**Post date:** [April 11, 2008, 3:27am UTC](https://www.ruby-forum.com/t/repeating-form/134335/8 "2008-04-11T03:27:47Z")

</div>

Just what you’d usually use.

Julian.

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)  
VIDEO #3 out NOW!  
[http://sensei.zenunit.com/](http://sensei.zenunit.com/)
