# Populating text\_tag with serialized data

**URL:** <https://www.ruby-forum.com/t/populating-text-tag-with-serialized-data/54953>\
**Category:** Rails\
**Created:** [March 15, 2006, 2:19pm UTC](https://www.ruby-forum.com/t/populating-text-tag-with-serialized-data/54953 "2006-03-15T14:19:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [March 15, 2006, 2:19pm UTC](https://www.ruby-forum.com/t/populating-text-tag-with-serialized-data/54953/1 "2006-03-15T14:19:53Z")

</div>

I am using a serialized attribute in one of my models The serialized  
data is  
in the form of an array. I want to have a text\_tag for each element  
contained in my serialized attribute. How would I construct the  
text\_tag?

My model name is ‘questions’. The serialized attribute is “answers”

So I might have:

\<% form tag %\>  
\<% text\_tag ‘question’, ‘name’ %\>  
\<% text\_tag ‘question’, ‘answers[0]’ %\>  
\<% text\_tag ‘question’, ‘answers[1]’ %\>

It’s the last two I can’t figure out. I’m sure there is a clean, simple  
way  
of doing it.

Slight complications - my stored array is actually an arrays within an  
array

- storing the answer\_name and correct\_or\_not. Some answers might have 2  
answers, while others might have 5.

Thanks very much,

Steve  
[http://www.smarkets.net](http://www.smarkets.net)

---

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [March 15, 2006, 3:23pm UTC](https://www.ruby-forum.com/t/populating-text-tag-with-serialized-data/54953/2 "2006-03-15T15:23:23Z")

</div>

Steve O. wrote:

> \<% text\_tag ‘question’, ‘name’ %\>  
> \<% text\_tag ‘question’, ‘answers[0]’ %\>  
> \<% text\_tag ‘question’, ‘answers[1]’ %\>
> 
> It’s the last two I can’t figure out. I’m sure there is a clean, simple  
> way of doing it.
> 
> Slight complications - my stored array is actually an arrays within an  
> array - storing the answer\_name and correct\_or\_not. Some answers might  
> have 2 answers, while others might have 5.

\<% form tag %\>  
\<% text\_field ‘question’, ‘name’ %\>  
\<% text\_field\_tag ‘question[answers][]’, @question.answers[0].first %\>  
\<% text\_field\_tag ‘question[answers][]’, @question.answers[1].first %\>

which when POSTed will give you an array of answer\_names in  
params[:question][:answers], into which you can (re-)insert  
the correct\_or\_not data.

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

---

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [March 15, 2006, 3:39pm UTC](https://www.ruby-forum.com/t/populating-text-tag-with-serialized-data/54953/3 "2006-03-15T15:39:12Z")

</div>

Excellent, Mark. Thank you.

Steve

---

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [March 15, 2006, 3:32pm UTC](https://www.ruby-forum.com/t/populating-text-tag-with-serialized-data/54953/4 "2006-03-15T15:32:48Z")

</div>

Steve O. wrote:

> \<% text\_tag ‘question’, ‘name’ %\>  
> \<% text\_tag ‘question’, ‘answers[0]’ %\>  
> \<% text\_tag ‘question’, ‘answers[1]’ %\>
> 
> It’s the last two I can’t figure out. I’m sure there is a clean, simple  
> way of doing it.
> 
> Slight complications - my stored array is actually an arrays within an  
> array - storing the answer\_name and correct\_or\_not. Some answers might  
> have 2 answers, while others might have 5.

\<%= form tag %\>  
\<%= text\_field ‘question’, ‘name’ %\>  
\<%= text\_field\_tag ‘question[answers][]’, @question.answers[0].first %\>  
\<%= text\_field\_tag ‘question[answers][]’, @question.answers[1].first %\>

which when POSTed will give you an array of answer\_names in  
params[:question][:answers], into which you can (re-)insert  
the correct\_or\_not data.

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

---

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [March 16, 2006, 12:07am UTC](https://www.ruby-forum.com/t/populating-text-tag-with-serialized-data/54953/5 "2006-03-16T00:07:06Z")

</div>

The format is text\_field(object, method… etc) so you need to put  
text\_field(‘question’, ‘answers\_container[]’,…) similar to how you  
did above it.

Julian.

---

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [March 16, 2006, 12:31am UTC](https://www.ruby-forum.com/t/populating-text-tag-with-serialized-data/54953/6 "2006-03-16T00:31:21Z")

</div>

Steve O. wrote:

> ## @question.answers\_container[0].first, :size =\> 75, :class =\> “thin\_borderGreen”) %\>

Note that in my answer I used text\_field\_tag rather than text\_field:

> ```
> <%= text_field_tag 'question[answers][]', @question.answers[0].first %>
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [March 15, 2006, 6:04pm UTC](https://www.ruby-forum.com/t/populating-text-tag-with-serialized-data/54953/7 "2006-03-15T18:04:06Z")

</div>

I spoke too soon. I’m getting this error message:

@question[answers\_container]’ is not allowed as an instance variable  
name

## on the second line below:

\<%= text\_area(“question”, “question\_text”, :cols=\>41, :rows=\>3, :class  
=\>  
“question\_input”) %\>

## \<%= text\_field(‘question[answers\_container][]’, @question.answers\_container[0].first, :size =\> 75, :class =\> “thin\_borderGreen”) %\>

My model look like this:  
=\> #\<Question:0x397c4e8 @attributes={“created\_on”=\>nil,  
“answers\_container”=\>nil  
, “updated\_on”=\>nil, “notes”=\>nil, “user\_id”=\>0, “ratings\_count”=\>0,  
“active”=\>"  
0", “question\_text”=\>nil}, @new\_record=true\>

Any ideas?

Steve  
[http://www.smarkets.net](http://www.smarkets.net)

---

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [March 16, 2006, 4:00am UTC](https://www.ruby-forum.com/t/populating-text-tag-with-serialized-data/54953/8 "2006-03-16T04:00:43Z")

</div>

Thanks to you both. The text\_field\_tag did it. I overlooked it  
previously.

Aussies seem to really know the form\_helper library. ; )

Steve  
[http://www.smarkets.net](http://www.smarkets.net)
