Forum: Ruby on Rails Help with AJAX, controller receiving extra 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.
7f30149100d05b8869b0ca351ef9e756?d=identicon&s=25 Vincent AE Scott (Guest)
on 2005-12-13 11:27
(Received via mailing list)
Hi there once again,

I've got stumped on something simple once again, I have the following
in a view:

<%= link_to_function 'Add another answer', "new
Insertion.Bottom('answerslist', $('answer_form').innerHTML)" %>

<ol id='answerslist'></ol>

<span id='answer_form' style='display:none;'>
        <li><%= text_field_tag "answers[]" %></li>
</span>

Which allows me to append new elements to answers[].
But the problem is that an empty string is always present at the end of
the array.  When its supposed to be empty (nothing added) there is
precisely one empty string, and when there's more than one answer in the
array, the last one is still an empty string.

At present I'm fixing this in the controller with the following:

    @answers = params[:answers]
    @answers.each{ |a| @question.answers << Answer.new( :description =>
a ) unless a == "" }

But it's not exactly fixing the actual problem.

I'm not actually even sure if this is a bug on my behalf, or if its just
the way its supposed to behave.

Can anyone put me right?

Cheers,
-v

--
keys:  http://codex.net/gpg.asc

 Scientist: A left-over from the age of reason (pre 1982)
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2005-12-13 12:16
(Received via mailing list)
Vincent AE Scott wrote:

> But the problem is that an empty string is always present at the end of
> the array.  When its supposed to be empty (nothing added) there is
> precisely one empty string, and when there's more than one answer in the
> array, the last one is still an empty string.

Is your 'answer_form' span inside your form or outside?  Make sure
it's outside.

--
We develop, watch us RoR, in numbers too big to ignore.
7f30149100d05b8869b0ca351ef9e756?d=identicon&s=25 Vincent AE Scott (Guest)
on 2005-12-13 12:34
(Received via mailing list)
#if mrj /* Dec 13, 10:11 */
> >
> >Which allows me to append new elements to answers[].
> >But the problem is that an empty string is always present at the end of
> >the array.  When its supposed to be empty (nothing added) there is
> >precisely one empty string, and when there's more than one answer in the
> >array, the last one is still an empty string.
>
> Is your 'answer_form' span inside your form or outside?  Make sure
> it's outside.
>
#endif /* mrj@bigpond.net.au */


Ahhhh, Bingo!  Cheers for that, makes perfect sense now.  Gotta love
this list :)

--
keys:  http://codex.net/gpg.asc

 Recession: Economic downturn entirely due to external factors
 Recovery: Economic upturn entirely due to government action
This topic is locked and can not be replied to.