Forum: Ruby on Rails Binding Text Field/Areas to Array?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3cbe6157fca13c0fbbb5ed81b803c555?d=identicon&s=25 Web Manager (jdw)
on 2006-04-12 23:37
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
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-04-13 03:24
(Received via mailing list)
Web Manager 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.
3dd1fa1ceabf75346ad720147877dbea?d=identicon&s=25 Guest (Guest)
on 2006-04-13 06:08
Thanks very much for your time and the suggestions,  Mark!  I'll give
these a try tomorrow...

Cheers,

DRoberts
3dd1fa1ceabf75346ad720147877dbea?d=identicon&s=25 Guest (Guest)
on 2006-04-13 06:45
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.
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-04-13 07:49
(Received via mailing list)
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.
This topic is locked and can not be replied to.