Binding Text Field/Areas to Array?

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

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.

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

Cheers,

DRoberts

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.

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.