# What would be the best way to

**URL:** <https://www.ruby-forum.com/t/what-would-be-the-best-way-to/111326>\
**Category:** Rails\
**Created:** [August 6, 2007, 3:16am UTC](https://www.ruby-forum.com/t/what-would-be-the-best-way-to/111326 "2007-08-06T03:16:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![brandon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f08c70/32.png) [@brandon](https://www.ruby-forum.com/u/brandon)\
**Post date:** [August 6, 2007, 3:16am UTC](https://www.ruby-forum.com/t/what-would-be-the-best-way-to/111326/1 "2007-08-06T03:16:01Z")

</div>

I have a model ‘assignment’ that has\_many of the model ‘answer’. What  
would be the best way to have a form to save an ‘assignment’ and some  
of its ‘answers’ all on the same form?

---

<div class="post-metadata">

**Author:** ![brandon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f08c70/32.png) [@brandon](https://www.ruby-forum.com/u/brandon)\
**Post date:** [August 6, 2007, 9:37am UTC](https://www.ruby-forum.com/t/what-would-be-the-best-way-to/111326/2 "2007-08-06T09:37:24Z")

</div>

Brandon wrote:

> I have a model ‘assignment’ that has\_many of the model ‘answer’. What  
> would be the best way to have a form to save an ‘assignment’ and some  
> of its ‘answers’ all on the same form?

given both models have three attributes, title, content & brief;  
i believe would this work (untested code)

text\_field ‘assignment’, ‘title’  
text\_field ‘assignment’, ‘content’  
text\_field ‘assignment’, ‘brief’  
text\_field ‘assignment’, ‘answer[title]’  
text\_field ‘assignment’, ‘answer[content]’  
text\_field ‘assignment’, ‘answer[brief]’

---

<div class="post-metadata">

**Author:** ![brandon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f08c70/32.png) [@brandon](https://www.ruby-forum.com/u/brandon)\
**Post date:** [August 7, 2007, 12:44am UTC](https://www.ruby-forum.com/t/what-would-be-the-best-way-to/111326/3 "2007-08-07T00:44:32Z")

</div>

On Aug 6, 2:37 am, Shai R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> given both models have three attributes, title, content & brief;  
> i believe would this work (untested code)
> 
> text\_field ‘assignment’, ‘title’  
> text\_field ‘assignment’, ‘content’  
> text\_field ‘assignment’, ‘brief’  
> text\_field ‘assignment’, ‘answer[title]’  
> text\_field ‘assignment’, ‘answer[content]’  
> text\_field ‘assignment’, ‘answer[brief]’

Hmm… that didn’t work. Also, I need to have more than 1 answer on  
the form. Maybe something like text fields with  
id=“assignment[answer][1][content]”?

---

<div class="post-metadata">

**Author:** ![brandon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f08c70/32.png) [@brandon](https://www.ruby-forum.com/u/brandon)\
**Post date:** [August 7, 2007, 3:14am UTC](https://www.ruby-forum.com/t/what-would-be-the-best-way-to/111326/4 "2007-08-07T03:14:44Z")

</div>

Hi –

On Mon, 6 Aug 2007, Brandon wrote:

> > text\_field ‘assignment’, ‘brief’  
> > text\_field ‘assignment’, ‘answer[title]’  
> > text\_field ‘assignment’, ‘answer[content]’  
> > text\_field ‘assignment’, ‘answer[brief]’
> 
> Hmm… that didn’t work. Also, I need to have more than 1 answer on  
> the form. Maybe something like text fields with  
> id=“assignment[answer][1][content]”?

You can give an arbitrary index to text fields, like this:

\<% @assignment.answers.each\_with\_index do |answer,i| %\>  
\<%= text\_field “answer”, “title”, :index =\> i %\>  
…  
\<% end %\>

You’ll then get params[:answer], which will be a hash with keys  
corresponding to the indices.

Maybe that, or something based on it, would give you what you need.

David

–

- Books:  
RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242](http://www.awprofessional.com/title/0321509242))  
RUBY FOR RAILS ([Ruby for Rails](http://www.manning.com/black))
- Ruby/Rails training  
& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))
