# Multiple objects of the same type with one form - REST

**URL:** <https://www.ruby-forum.com/t/multiple-objects-of-the-same-type-with-one-form-rest/87438>\
**Category:** Rails\
**Created:** [March 2, 2007, 4:47pm UTC](https://www.ruby-forum.com/t/multiple-objects-of-the-same-type-with-one-form-rest/87438 "2007-03-02T16:47:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [March 2, 2007, 4:47pm UTC](https://www.ruby-forum.com/t/multiple-objects-of-the-same-type-with-one-form-rest/87438/1 "2007-03-02T16:47:39Z")

</div>

I’ve looked at all the tutorials on how to create multiple objects of  
the same type with one form. However, I cannot get it to work with my  
REST app. This is what I have tried:  
----- Controller ---------

‘new’ action ----------  
def new  
@students = []  
5.times { @students \<\< Student.new }  
end

‘create’ action ----------  
params[:students].values do |student|  
Student.new(student) unless student.values.all?(&:blank?)  
end

-------- View -----------  
\<% @students.each\_with\_index do |student, index| %\>  
\<% fields\_for “students[#{index}]”, student do |s| %\>  
\<%= render :partial =\> ‘students/student\_form’, :locals =\>  
{:f =\> s} %\>  
\<% end %\>  
\<% end %\>

I cannot figure out why this is not working. All the student objects  
are correctly in the parameters but they will not get saved. Please  
help.

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [March 2, 2007, 4:53pm UTC](https://www.ruby-forum.com/t/multiple-objects-of-the-same-type-with-one-form-rest/87438/2 "2007-03-02T16:53:40Z")

</div>

> ‘create’ action ----------  
> params[:students].values do |student|  
> Student.new(student) unless student.values.all?(&:blank?)  
> end

You aren’t saving the Student? Either by calling Student.save after or  
using .create instead of .new. I believe.

Regards,  
Andrew

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [March 2, 2007, 8:23pm UTC](https://www.ruby-forum.com/t/multiple-objects-of-the-same-type-with-one-form-rest/87438/3 "2007-03-02T20:23:16Z")

</div>

Here is my full create method:

def create  
params[:students].values do |student|  
Student.new(student) unless student.values.all?(&:blank?)  
end

```
respond_to do |format|
  if @????.save
    flash[:notice] = 'Students were successfully created.'
    format.html { redirect_to students_path }
  else
    format.html { render :action => "new" }
  end
end

```

end

As you can see, since I do not have an instance variable, I don’t know  
what to put in the if clause. But even if I replace new with create,  
it still doesn’t create the objects. I’ve tried getting rid of the  
respond\_to just to see if the object would be created but no luck.

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [March 3, 2007, 2:10am UTC](https://www.ruby-forum.com/t/multiple-objects-of-the-same-type-with-one-form-rest/87438/4 "2007-03-03T02:10:59Z")

</div>

Is something like possible with REST?

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [March 4, 2007, 12:26pm UTC](https://www.ruby-forum.com/t/multiple-objects-of-the-same-type-with-one-form-rest/87438/5 "2007-03-04T12:26:48Z")

</div>

Everything works perfectly now after I followed this tutorial:  
[http://www.railsforum.com/viewtopic.php?id=1063](http://www.railsforum.com/viewtopic.php?id=1063)

Here is a tip for fellow newbies: Get your model relationships  
correct. Read the ActiveRecord chapters in AWDwR thoroughly.

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [March 3, 2007, 7:22am UTC](https://www.ruby-forum.com/t/multiple-objects-of-the-same-type-with-one-form-rest/87438/6 "2007-03-03T07:22:16Z")

</div>

Finally, I got the objects to save! Here is my new create method:

def create  
params[:students].each\_value do |student|  
@student = Student.new(student) unless student.values.all?  
(&:blank?)  
@student.save  
end  
flash[:notice] = ‘Family was successfully created.’  
redirect\_to student\_url(@student)  
end

Now, as you can see, I had to get rid of my respond\_to block. How can  
I handle errors using the if clause since the values of @student  
change with each object?
