# Create multiple new objects in one page

**URL:** <https://www.ruby-forum.com/t/create-multiple-new-objects-in-one-page/136769>\
**Category:** Rails\
**Created:** [May 6, 2008, 8:15am UTC](https://www.ruby-forum.com/t/create-multiple-new-objects-in-one-page/136769 "2008-05-06T08:15:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![shenry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shenry](https://www.ruby-forum.com/u/shenry)\
**Post date:** [May 6, 2008, 8:15am UTC](https://www.ruby-forum.com/t/create-multiple-new-objects-in-one-page/136769/1 "2008-05-06T08:15:58Z")

</div>

I’m trying to make a view with one form for each instance of a model  
(ferms) in which a user can update some attributes (in this case I’m  
calling them kinetics)

Ferm  
has\_many :kinetics

Kinetic  
belongs\_to :ferm

In my KineticsController I have before\_filter :get\_ferms

private  
def get\_ferms  
@ferms = Ferms.find(:all)  
end  
In my new action in the KineticsController I have

@kinetic = Kinetic.new

And that’s where I get stuck…

Here is what I have in the views/kinetics/new.html.erb:

\<% @ferms.each do |ferm| %\>

**Name :** \<%= ferm.name %\>

\<% form\_for(ferm, @kinetic) do |f| %\>

**\<Something** \<%= f.text\_field :something %\>

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

But it’s broken… Any ideas on what to change/do differently to  
create the page with multiple forms?? Thanks for any help,

SH

---

<div class="post-metadata">

**Author:** ![shenry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shenry](https://www.ruby-forum.com/u/shenry)\
**Post date:** [May 6, 2008, 8:40am UTC](https://www.ruby-forum.com/t/create-multiple-new-objects-in-one-page/136769/2 "2008-05-06T08:40:43Z")

</div>

watch the railscasts on complex forms:

Part 1 [#73 Complex Forms Part 1 - RailsCasts](http://railscasts.com/episodes/73)  
Part 2 [#74 Complex Forms Part 2 - RailsCasts](http://railscasts.com/episodes/74)  
Part 3 [#75 Complex Forms Part 3 - RailsCasts](http://railscasts.com/episodes/75)

---

<div class="post-metadata">

**Author:** ![shenry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shenry](https://www.ruby-forum.com/u/shenry)\
**Post date:** [June 9, 2008, 1:44am UTC](https://www.ruby-forum.com/t/create-multiple-new-objects-in-one-page/136769/3 "2008-06-09T01:44:57Z")

</div>

I think I’m close to figuring it out… below are the relevant app  
details, any suggestions are greatly appreciated.

Models:

class Ferm \< AR::Base  
has\_many :kinetics, :dependent =\> :destroy  
end

class Kinetic \< AR::Base  
belongs\_to :ferm  
end

Controllers

class KineticsController \< AC  
def new  
@kinetics = []  
@ferms = Ferm.find(:all)  
for ferm in @ferms  
kinetic = ferm.kinetics.build(params[:kinetic])  
@kinetics \<\< kinetic  
end  
end

def create  
@kinetics = params[:kinetic]  
for kinetic in @kinetics  
new\_kinetic = Kinetic.new(params[:kinetic])  
new\_kinetic.save  
end  
redirect\_to :controller =\> ‘ferms’, :action =\> ‘index’  
end

View

Kinetics/new.html.erb

```
<% form_tag(:action => 'create') do -%>

```

\<%= render :partial =\> ‘kinetic’, :collection =\> @kinetics %\>  
\<%= submit\_tag ‘Enter Brix & Temp’ %\>  
\<% end -%\>

Kinetics/\_kinetic.html.erb

```
<table>

```

 Tank \<%=h kinetic.ferm.tank %\> Blend \<%=h kinetic.ferm.blend\_number %\> Brix \<%= text\_field(:kinetic, :brix, :name =\> "kinetic[][brix]") %\> Temp \<%= text\_field(:kinetic, :temp, :name =\> "kinetic[][temp]") %\> \<%= hidden\_field\_tag(:kinetic, :ferm\_id, :name =\> "kinetic[] [ferm\_id]", :value =\> kinetic.ferm.id) %\> 

…

What this gives me in the params hash is: parameters:  
{“commit”=\>“Enter Brix & Temp”, “kinetic”=[{“ferm\_id”=\>[1],  
“brix”=\>[25], “temp”=\>[70]},{“ferm\_id”=\>[2], “brix”=\>[28],  
“temp”=\>[80]}] and I get the error: NoMethodError in  
KineticsController#create … undefined method `stringify\_keys!’ for  
#Array:0x198b1dc.

I understand that stringify\_keys! is expecting a hash and getting an  
array and that’s why it’s complaining… am I instantiating the  
@kinetics variable incorrectly in KineticsController#new to handle  
this array? Should I reconfigure the form to submit a hash of the  
kinetics (like: {“1”=\>[{“ferm\_id”=\>“1”, “brix”=\>“25”,  
“temp”=\>“70”}]})?

Any help is appreciated… thanks in advance.

SH

---

<div class="post-metadata">

**Author:** ![shenry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shenry](https://www.ruby-forum.com/u/shenry)\
**Post date:** [June 9, 2008, 6:04am UTC](https://www.ruby-forum.com/t/create-multiple-new-objects-in-one-page/136769/4 "2008-06-09T06:04:17Z")

</div>

figured it out… my problem was in the new action in the  
KineticsController. Here is what has worked:

def new  
@kinetics = params[:kinetic]  
@kinetics.each do |k|  
new\_kinetic = Kinetic.new  
new\_kinetic.brix = k[“brix”]  
new\_kinetic.temp = k[“temp”]  
new\_kinetic.ferm\_id = k[“ferm\_id”]  
new\_kinetic.save  
end  
end

I’m sure there are several better ways to do this, please chime in if  
anyone wants to share an improvement to this method.

SH

---

<div class="post-metadata">

**Author:** ![shenry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shenry](https://www.ruby-forum.com/u/shenry)\
**Post date:** [June 9, 2008, 1:50am UTC](https://www.ruby-forum.com/t/create-multiple-new-objects-in-one-page/136769/5 "2008-06-09T01:50:13Z")

</div>

> What this gives me in the params hash is: parameters:  
> {“commit”=\>“Enter Brix & Temp”, “kinetic”=[{“ferm\_id”=\>[1],  
> “brix”=\>[25], “temp”=\>[70]},{“ferm\_id”=\>[2], “brix”=\>[28],  
> “temp”=\>[80]}] and I get the error: NoMethodError in  
> KineticsController#create … undefined method `stringify\_keys!’ for  
> #Array:0x198b1dc.

Oops typo above: the array looks like :

“kinetic”=[{“ferm\_id”=\>“1”, “brix”=\>“25”, “temp”=\>“70”},  
{“ferm\_id”=\>“2”, “brix”=\>“28”, “temp”=\>“80”}]

without square brackets around the values.
