Hi all, I have to implement the functionality where I have to insert multiple rows at the same time in the table. The functionality is implemented but it is taking only the first row multiple times. The view is: <td>Quantity</td> <% @units.each do |unit| %> <td><input id = "quantity" name="productprice[quantity]" type="text" value="<%= unit.units%>"></td> <td><input id ="price" name="productprice[price]" type="text" value="" /></td><br /> <% end %> The controller is: def add_price @product=Product.find(params["id"]) @units = Unit.find :all @length = @units.length @length.times do @productprice = Productprice.new(params[:productprice]) if @productprice.save end end end Please give me the solution of the above problem. Thanks and Regards, Rohit. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thorsten Mueller
2007-Nov-26 11:02 UTC
Re: Inserting multiple rows in the table at same time
<input id = "quantity" name="productprice[quantity]"... maybe, all your inputs get the same id and name and that''s the source of trouble? especially if the enclosing form has autocomplete on... try something like id="quantity_#{unit.id}" you''ll need different id''s anyway, to get the right data in the receiving controller -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thorsten Mueller wrote:> <input id = "quantity" name="productprice[quantity]"... > > maybe, all your inputs get the same id and name and that''s the source of > trouble? especially if the enclosing form has autocomplete on... > > try something like id="quantity_#{unit.id}" > > you''ll need different id''s anyway, to get the right data in the > receiving controllerYeah you r rite. All the inputs had the same id and name. Now my view looks like: <% i = 0 %> <% @units.each do |unit| %> <% i = i+1 %> <td><input id = "quantity<%= i %>_i" name="productprice[quantity]" type="text" value="<%= unit.units%>"></td> <td><input id ="price_i<%= i %>" name="productprice[price]" type="text" value="" /></td><br /> <% end %> Now the ids generated are quantity1,price1 and quantity2, price2. But am not able to access these in my controller. Please help me out. Thanks, Rohit. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thorsten Mueller
2007-Nov-26 11:30 UTC
Re: Inserting multiple rows in the table at same time
this depend on some details, mainly how the form is created and named normaly you would get your params like params[:form_name][:field_name] so if you have something like form_for :units then params[:units][:quantity_1] should give you a result otherwise look in you development.log. after submitting your form it should look something like this: (the Parameters part is of interest) Processing UsersController#create (for 127.0.0.1 at 2007-11-26 12:08:32) [POST] Session ID: Parameters: {"user"=>{"name"=>"q", "city"=>"q", "zip"=>"q", "password_confirmation"=>"qqqq", "country_id"=>"1", "address_two"=>"q", "phone"=>"q", "first_name"=>"q", "address_one"=>"q", "password"=>"qqqq", "state"=>"q", "email"=>"qqq-WuipUPfaM4g@public.gmane.org"}, "commit"=>"Save", "action"=>"create", "controller"=>"users", "redirect"=>""} in this case it was for a form ''user'' so params[:user][:email] would give the result qqq-WuipUPfaM4g@public.gmane.org -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thorsten Mueller wrote:> this depend on some details, mainly how the form is created and named > > normaly you would get your params like params[:form_name][:field_name] > so if you have something like form_for :units > > then > params[:units][:quantity_1] > > should give you a result > > otherwise look in you development.log. after submitting your form it > should look something like this: > (the Parameters part is of interest) > > Processing UsersController#create (for 127.0.0.1 at 2007-11-26 12:08:32) > [POST] > Session ID: Parameters: {"user"=>{"name"=>"q", "city"=>"q", > "zip"=>"q", "password_confirmation"=>"qqqq", "country_id"=>"1", > "address_two"=>"q", "phone"=>"q", "first_name"=>"q", "address_one"=>"q", > "password"=>"qqqq", "state"=>"q", "email"=>"qqq-WuipUPfaM4g@public.gmane.org"}, > "commit"=>"Save", "action"=>"create", "controller"=>"users", > "redirect"=>""} > > in this case it was for a form ''user'' > so params[:user][:email] would give the result qqq-WuipUPfaM4g@public.gmane.orghi, The view is: <% i = 0 %> <% @units.each do |unit| %> <% i = i + 1 %> <td><input id = "quantity<%= i %>" name="productprice[quantity]" type="text" value="<%= unit.units%>"></td> <td><input id ="price<%= i %>" name="productprice[price]" type="text" value="" /></td><br /> <% end %> Controller is: @product=Product.find(params["id"]) @units = Unit.find :all @length = @units.length @length.times do @productprice = Productprice.new(params[:productprice]) if @productprice.save end end Th parameters are: "productprice"=>{"price"=>"2", "quantity"=>"50", "product_id"=>"3"} Can you tell me where is the error? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thorsten Mueller
2007-Nov-26 11:59 UTC
Re: Inserting multiple rows in the table at same time
name must be unique, too in your view: name="productprice[quantity_#{unit.id}]" for which then you would have to change your controller to go through all fields -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thorsten Mueller wrote:> name must be unique, too > in your view: > > name="productprice[quantity_#{unit.id}]" > > for which then you would have to change your controller to > go through all fieldsThanks a lot for replying. I have changed my view. <% i = 0 %> <% @units.each do |unit| %> <% i = i + 1 %> <td><input id = "quantity<%= i %>" name="productprice[quantity<%= i %>]" type="text" value="<%= unit.units%>"></td> <td><input id ="price<%= i %>" name="productprice[price<%= i %>]" type="text" value="" /></td><br /> <% end %> </tr> The controller is: def add_price @product=Product.find(params["id"]) @units = Unit.find :all @length = @units.length @length.times do @productprice = Productprice.new(params[:productprice]) if @productprice.save end end Now the parameters are: "productprice"=>{"product_id"=>"3", "quantity1"=>"50", "quantity2"=>"1000", "price1"=>"3", "price2"=>"4"}} But it is giving error: undefined method `quantity1='' for #<Productprice:0x466f450> Because it might be looking in the database for field named quantity1 and its not getting. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rohit Mehra wrote:> Thorsten Mueller wrote: >> name must be unique, too >> in your view: >> >> name="productprice[quantity_#{unit.id}]" >> >> for which then you would have to change your controller to >> go through all fields > > Thanks a lot for replying. > I have changed my view. > <% i = 0 %> > <% @units.each do |unit| %> > <% i = i + 1 %> > <td><input id = "quantity<%= i %>" name="productprice[quantity<%= i > %>]" type="text" value="<%= unit.units%>"></td> > <td><input id ="price<%= i %>" name="productprice[price<%= i %>]" > type="text" value="" /></td><br /> > <% end %> > </tr> > > The controller is: > def add_price > @product=Product.find(params["id"]) > @units = Unit.find :all > @length = @units.length > @length.times do > @productprice = Productprice.new(params[:productprice]) > if @productprice.save > end > end > > Now the parameters are: > "productprice"=>{"product_id"=>"3", "quantity1"=>"50", > "quantity2"=>"1000", "price1"=>"3", "price2"=>"4"}} > > But it is giving error: > undefined method `quantity1='' for #<Productprice:0x466f450> > > Because it might be looking in the database for field named quantity1 > and its not getting.hi, Please provide me the solution of this problem. Thanks, Rohit. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thorsten Mueller
2007-Nov-26 14:16 UTC
Re: Inserting multiple rows in the table at same time
> Because it might be looking in the database for field named quantity1that''s right, so far you can''t use form_for in that case, but must fall back to the normal form_tag instead, then manually going through the returned parameters hash by generating the hash-keys rg params[productprice]["price#{counter}"] there are several ways to handle a problem like this, using one form with multiple numbered fields or using multiple forms and keep the fieldnames (and submit-buttons). it''s up to you, to make the right decision. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thorsten Mueller wrote:>> Because it might be looking in the database for field named quantity1 > that''s right, so far > > you can''t use form_for in that case, but must fall back to the normal > form_tag instead, then manually going through the returned parameters > hash by generating the hash-keys rg > params[productprice]["price#{counter}"] > > there are several ways to handle a problem like this, using one form > with multiple numbered fields or using multiple forms and keep the > fieldnames (and submit-buttons). it''s up to you, to make the right > decision.Hi, My controller is: @product=Product.find(params["id"]) @units = Unit.find :all i = 0 @units.each do |unit| i=i+1 @productprice = Productprice.new(params[:productprice]) @productprice.quanity = request.parameters["quanity#{i}"] @productprice.quanity = request.parameters["unit#{i}"] end end But its giving error: undefined method `quanity='' for # Please tell me wher i am wrong? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks a lot for help. The code is working now. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---