Forum: Ruby on Rails Populating text_tag with serialized data

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.
Steve O. (Guest)
on 2006-03-15 15:19
(Received via mailing list)
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
Mark Reginald J. (Guest)
on 2006-03-15 16:23
(Received via mailing list)
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.
Mark Reginald J. (Guest)
on 2006-03-15 16:32
(Received via mailing list)
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.
Steve O. (Guest)
on 2006-03-15 16:39
(Received via mailing list)
Excellent, Mark. Thank you.

Steve
Steve O. (Guest)
on 2006-03-15 19:04
(Received via mailing list)
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
Julian L. (Guest)
on 2006-03-16 01:07
(Received via mailing list)
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.
Mark Reginald J. (Guest)
on 2006-03-16 01:31
(Received via mailing list)
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.
Steve O. (Guest)
on 2006-03-16 05:00
(Received via mailing list)
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
This topic is locked and can not be replied to.