Ajax and creation of a list


#1

The goal is to create the content of a list from a single page, with
ajax.

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’ %>

Telephone

<%= link_to_remote("Add another number",
                    :url=>{:action=>'new_tel'},
                     :update =>'telephone', :position=> :after)
 %>
<br/>

<%= text_field_tag ‘telephone’ %>

<%= end_form_tag %>

in the controller :

def new_tel
@tel_num||=0
@tel_num=@tel_num.succ
render (:text=>"
"+@tel_num.to_s+’<input
id=“telephone”’+@tel_num.to_s+’ name=“telephone” type=“text” />’)
end

Several problem with this solution :
_ it doesn’t work :slight_smile:
_ 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.

Pierre