Populating text_tag with serialized data

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

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.

Excellent, Mark. Thank you.

Steve

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.

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.

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.

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

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