Neeraj Kumar
2006-Jul-21 17:35 UTC
[Rails] How to dynamically add more rows and submit data
Let''s assume that I am trying to build an application where the user can enter invoice. In the app the user can add lines items. Each line item has decription, date, rate and hours. When the invoice screen comes up the first time I have one blank row for the line items. <tr> <th>Description</th> <th>Date</th> <th>Hours </th> <th>Rate </th> </tr> <tr> <td><%= text_field ''lineitem'',''desc-1'' %></td> <td><%= text_field ''lineitem'',''date-1'' %></td> <td><%= text_field ''lineitem'',''hours-1''%></td> <td><%= text_field ''lineitem'',''rate-1'' ></td> </tr> Now the user wants to add another line. Using ajax I create one more row. <tr> <td><%= text_field ''lineitem'',''desc-2'' %></td> <td><%= text_field ''lineitem'',''date-2'' %></td> <td><%= text_field ''lineitem'',''hours-2''%></td> <td><%= text_field ''lineitem'',''rate-2'' ></td> </tr> But the problem is that the model Lineitem has following attributes: desc,date,hours and rate. What is the best way to handle such cases. I don''t want to have items like desc-1 and rate-2. Thanks. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060721/bdafc57a/attachment.html
Mark Reginald James
2006-Jul-22 04:01 UTC
[Rails] Re: How to dynamically add more rows and submit data
Neeraj Kumar wrote:> Let''s assume that I am trying to build an application where the user can > enter invoice. > > In the app the user can add lines items. Each line item has decription, > date, rate and hours. > > When the invoice screen comes up the first time I have one blank row for > the line items. > > <tr> > <th>Description</th> > <th>Date</th> > <th>Hours </th> > <th>Rate </th> > </tr> > > <tr> > <td><%= text_field ''lineitem'',''desc-1'' %></td> > <td><%= text_field ''lineitem'',''date-1'' %></td> > <td><%= text_field ''lineitem'',''hours-1''%></td> > <td><%= text_field ''lineitem'',''rate-1'' ></td> > </tr> > > Now the user wants to add another line. Using ajax I create one more row. > > <tr> > <td><%= text_field ''lineitem'',''desc-2'' %></td> > <td><%= text_field ''lineitem'',''date-2'' %></td> > <td><%= text_field ''lineitem'',''hours-2''%></td> > <td><%= text_field ''lineitem'',''rate-2'' ></td> > </tr> > > > But the problem is that the model Lineitem has following attributes: > desc,date,hours and rate. > > > What is the best way to handle such cases. I don''t want to have items > like desc-1 and rate-2.You want <%= text_field :lineitem, :desc, :index => i %>, which is sort of equivalent to <%= text_field_tag "lineitem[#{i}][desc]" %>, giving you suitable sets of lineitem attributes in numbered keys of the lineitem hash in params. An alternative is <%= text_field_tag ''lineitem[desc][]'' %>, etc., which will give you a set of attribute arrays that you can collate into objects in your controller. The advantage of this approach is that there''s no numbering involved, which makes it easier to avoid using AJAX and simply add the new row using a Javascript/DOM clone of an existing one -- much more responsive for the user. -- We develop, watch us RoR, in numbers too big to ignore.