Hi all, Let''s say you have People and Cars.. and every person can have multiple cars... I want to have the new person form use ajax to add/remove as many cars as required and have the user fill in that data inline... Without Ajax I would have the user select "5 cars", refresh the page.. have 5 car objects created and added to person.cars and then render using a collection of partials.. Attempting ajax I have no idea.. position :below works great to add new fields but I have no way of knowing what car I am on when I am rendering the fields for the cars (the server side doesn''t know if it''s the 6th car).. so I can''t use person_cars[6].type or anything like that.. Deleting is even worse... Any help would be greatly appreciated..
Hi all, Let''s say you have People and Cars.. and every person can have multiple cars... I want to have the new person form use ajax to add/remove as many cars as required and have the user fill in that data inline... Without Ajax I would have the user select "5 cars", refresh the page.. have 5 car objects created and added to person.cars and then render using a collection of partials.. Attempting ajax I have no idea.. position :below works great to add new fields but I have no way of knowing what car I am on when I am rendering the fields for the cars (the server side doesn''t know if it''s the 6th car).. so I can''t use person_cars[6].type or anything like that.. Deleting is even worse... Any help would be greatly appreciated..
I''ve managed to stumble through this a bit.. What I decided on was rendering each element of the collection using render_partial and using [] to identify each.. of coarse date_select doesn''t work with ''[]'' so that sucks.. but the problem is that when I have the user click "add" i just want to add a new set of inputs to the end of the list.. but I don''t have an ID for that new element... I could do something ugly like have a javascript variable in the main page that increments client side each time they hit new and passes that along to the ajax handler in the controller so that it can assign an incrementing new id.. Is there a better way of doing this? Thanks in advance! On 5-Aug-05, at 12:40 PM, Luke Galea wrote:> Hi all, > > Let''s say you have People and Cars.. and every person can have > multiple cars... > I want to have the new person form use ajax to add/remove as many > cars as required and have the user fill in that data inline... > > Without Ajax I would have the user select "5 cars", refresh the > page.. have 5 car objects created and added to person.cars and then > render using a collection of partials.. > > Attempting ajax I have no idea.. position :below works great to add > new fields but I have no way of knowing what car I am on when I am > rendering the fields for the cars (the server side doesn''t know if > it''s the 6th car).. so I can''t use person_cars[6].type or anything > like that.. > Deleting is even worse... > > Any help would be greatly appreciated.. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >
Luke, If I am understanding your problem correctly, then I have a (decent) solution. In my app I have many articles which each have their own comment form. It comes out like this: <% target_div = "article_line_write_comment_" << @article.id.to_s -%> <div class="write_comment"> <%= link_to_remote( "Write a Comment", :update => target_div, :complete => "new Effect.BlindDown(''" << target_div << "'')", :url =>{ :controller => ''archive'', :action => :write_comment, :id => @article.id , :target => "article_line_comments_" << @article.id.to_s } ) %> </div> <div id=''<%= target_div %>''></div> Let Rails do the magic, no JS needed. I think a similar approach should be able to handle your car situation..? Hope this helps, feel free to ask questions. -Jeff Luke Galea wrote:> I''ve managed to stumble through this a bit.. What I decided on was > rendering each element of the collection using render_partial and using > [] to identify each.. of coarse date_select doesn''t work with ''[]'' so > that sucks.. > > but the problem is that when I have the user click "add" i just want to > add a new set of inputs to the end of the list.. but I don''t have an ID > for that new element... > I could do something ugly like have a javascript variable in the main > page that increments client side each time they hit new and passes that > along to the ajax handler in the controller so that it can assign an > incrementing new id.. > > Is there a better way of doing this? > > Thanks in advance! >
Thanks Jeff.. What if you wanted multiple comments to be posted at once? That''s where it gets ugly as far as I can tell.. Because if you were to click "write comment" multiple times and use :position => :after then there would be no way of identifying one comment field versus another.. would there? On Friday 05 August 2005 15:49, Jeff Casimir wrote:> Luke, > > If I am understanding your problem correctly, then I have a (decent) > solution. > > In my app I have many articles which each have their own comment form. > It comes out like this: > > <% target_div = "article_line_write_comment_" << @article.id.to_s -%> > <div class="write_comment"> > <%= link_to_remote( "Write a Comment", > > :update => target_div, > :complete => "new Effect.BlindDown(''" << target_div << > > "'')", > > :url =>{ > : > :controller => ''archive'', > :action => :write_comment, > :id => @article.id , > :target => "article_line_comments_" << @article.id.to_s > > } > ) %> > </div> > <div id=''<%= target_div %>''></div> > > Let Rails do the magic, no JS needed. I think a similar approach should > be able to handle your car situation..? > > Hope this helps, feel free to ask questions. > > -Jeff > > Luke Galea wrote: > > I''ve managed to stumble through this a bit.. What I decided on was > > rendering each element of the collection using render_partial and using > > [] to identify each.. of coarse date_select doesn''t work with ''[]'' so > > that sucks.. > > > > but the problem is that when I have the user click "add" i just want to > > add a new set of inputs to the end of the list.. but I don''t have an ID > > for that new element... > > I could do something ugly like have a javascript variable in the main > > page that increments client side each time they hit new and passes that > > along to the ajax handler in the controller so that it can assign an > > incrementing new id.. > > > > Is there a better way of doing this? > > > > Thanks in advance! > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails