Forum: Ruby on Rails ajax and creation of a list

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Pierre Gambarotto (Guest)
on 2006-02-01 18:33
(Received via mailing list)
The goal is to create the content of a list from a single page, with

For instance,  a person "has many" telephones numbers, a telephone
number belongs to a person.
During the creation of the person, at first a single input box is
printed to insert a telephone number.

The user has then to click on a button (add another number) or to
edit the first box to make a second input box to display.
The form is updated with a new input box. At the end, the submission
of the form has as many numbers as desired by the user.

What I have done :
in the view :

<%= start_form_tag :action => 'create' %>

   <p><label for="telephone">Telephone</label>

    <%= link_to_remote("Add another number",
                         :update =>'telephone', :position=> :after)
<%= text_field_tag 'telephone' %></p>

<%= end_form_tag %>

in the controller :

def new_tel
     render (:text=>"<br/>"+@tel_num.to_s+'<input
id="telephone"'+@tel_num.to_s+' name="telephone" type="text" />')

Several problem with this solution :
_ it doesn't work :-)
_ from what I understand, each call to the remote action first
initializes a new controller, so the @tel_num variable is recreated
each time.
I should store the variable somewhere else. In the request maybe, but
I don't know how to do that.
_ Not very elegant. Each telephone is identified by a different name
(telephone1, telephone2, ...)
Is it possible to use an array instead ?

Thanks in advance for any clue, and sorry for the numerous errors in
my terrible written english.

This topic is locked and can not be replied to.