# Binding Text Field/Areas to Array?

**URL:** <https://www.ruby-forum.com/t/binding-text-field-areas-to-array/57680>\
**Category:** Rails\
**Created:** [April 12, 2006, 11:37pm UTC](https://www.ruby-forum.com/t/binding-text-field-areas-to-array/57680 "2006-04-12T23:37:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dbc845/32.png) [@jdw](https://www.ruby-forum.com/u/jdw)\
**Post date:** [April 12, 2006, 11:37pm UTC](https://www.ruby-forum.com/t/binding-text-field-areas-to-array/57680/1 "2006-04-12T23:37:43Z")

</div>

Hi,

If I have @answers in my Controller, which is an array of Answer model  
objects which don’t exist in the DB yet…

How do I bind a text\_area helper in the View to each Answer? (I’m  
pre-filling some of the Answers with other data people need to verify  
before submitting and will insert on POST.)

I’ve been able to get the textareas to render but not with the Answer  
values bound.

I’ve tried David’s ADWRR "add ‘[]’ to the name which gives errors, I  
think because there is no PK for the records yet.

I’ve also tried adding “index” =\> @Answers.id to the text\_area, but that  
fails as well.

Any help is very much appreciated!

DR

---

<div class="post-metadata">

**Author:** ![jdw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dbc845/32.png) [@jdw](https://www.ruby-forum.com/u/jdw)\
**Post date:** [April 13, 2006, 3:24am UTC](https://www.ruby-forum.com/t/binding-text-field-areas-to-array/57680/2 "2006-04-13T03:24:54Z")

</div>

Web M. wrote:

> I’ve tried David’s ADWRR "add ‘[]’ to the name which gives errors, I  
> think because there is no PK for the records yet.
> 
> I’ve also tried adding “index” =\> @Answers.id to the text\_area, but that  
> fails as well.

Two ways. Either:

\<% @answers.each do |answer| %\>  
\<%= text\_area\_tag ‘answers[]’, answer.text %\>  
\<% end %\>

which will post an array of responses, or

\<% @answers.each\_with\_index |@answer, i| %\>  
\<%= text\_area :answer, :text, :index =\> i %\>  
\<% end %\>

which will post a hash of answers with answer number  
keys. If you just want their values, operate on  
answer.values in your controller.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![jdw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dbc845/32.png) [@jdw](https://www.ruby-forum.com/u/jdw)\
**Post date:** [April 13, 2006, 6:08am UTC](https://www.ruby-forum.com/t/binding-text-field-areas-to-array/57680/3 "2006-04-13T06:08:58Z")

</div>

Thanks very much for your time and the suggestions, Mark! I’ll give  
these a try tomorrow…

Cheers,

DRoberts

---

<div class="post-metadata">

**Author:** ![jdw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dbc845/32.png) [@jdw](https://www.ruby-forum.com/u/jdw)\
**Post date:** [April 13, 2006, 7:49am UTC](https://www.ruby-forum.com/t/binding-text-field-areas-to-array/57680/4 "2006-04-13T07:49:21Z")

</div>

Guest wrote:

> Ok, thanks very much again Mark. The 2nd version above worked… I  
> think the :method approach is what was needed (I was using “method”).
> 
> The only change I made was to use brackets as below:
> 
> \<% @answers.each\_with\_index {|@answer, i| %\>  
> \<%= text\_area :answer, :answer, :index =\>  
> @answers[i].id\_projects\_coi\_questions, “cols” =\> 80, “rows” =\> 5, %\>  
> \<% } %\>

Yeah sorry, I forgot the “do” in this one.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![jdw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dbc845/32.png) [@jdw](https://www.ruby-forum.com/u/jdw)\
**Post date:** [April 13, 2006, 6:45am UTC](https://www.ruby-forum.com/t/binding-text-field-areas-to-array/57680/5 "2006-04-13T06:45:21Z")

</div>

Ok, thanks very much again Mark. The 2nd version above worked… I  
think the :method approach is what was needed (I was using “method”).

The only change I made was to use brackets as below:

\<% @answers.each\_with\_index {|@answer, i| %\>  
\<%= text\_area :answer, :answer, :index =\>  
@answers[i].id\_projects\_coi\_questions, “cols” =\> 80, “rows” =\> 5, %\>  
\<% } %\>

All best.
