Forum: Ruby on Rails ajax and creation of a list

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.
92965416368b1e311907226abdcfac68?d=identicon&s=25 Pierre Gambarotto (Guest)
on 2006-02-01 17:33
(Received via mailing list)
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' %>

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

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

<%= end_form_tag %>

in the controller :

def new_tel
    @tel_num||=0
     @tel_num=@tel_num.succ
     render (:text=>"<br/>"+@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 :-)
_ 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
This topic is locked and can not be replied to.