Prashant Tiwari
2006-Mar-17 11:53 UTC
[Rails] How to update values in database in this form?
Hi, This is my rHtml page & Corresponding actions & model pages:- ===listform.rHtml================<h1>Products List</h1> <%= start_form_tag(:action => "add_to_cart")%> <table> <tr> <% i=0 %> <th> Product No. </th> <th> Unit Price </th> <th> Available Qty. </th> <th> Demand Qty. </th> </tr> <% for product in @products %> <tr> <td> <%=hidden_field ''product'',''product_id''+i.to_s, :value=>product.id%> <%=product.name%> </td> <td> <%=product.unit_price%> </td> <td> <%=product.quantity%> </td> <td> <%=text_field("product", "dem_quantity"+i.to_s, "size" => 20)%> </td> </tr> <% i=i+1 %> <% end%> <tr> <%=hidden_field ''product'',''count'', :value=>i.to_i-1%></td> <td colspan=4 align="center"> <%= submit_tag("Add to Cart") %> </td> </tr> </table> <%= end_form_tag %> =======================================products_controller.rb====== def add_to_cart count=params[:product][:count] while count.to_i>=0.to_i @product = Product.find(params[:product][''product_id''+count.to_s]) print "pppp" + params[:product][''dem_quantity''+count.to_s] if params[:product][''dem_quantity''+count.to_s]=="" print @product.name else if @product.update_attributes(params[:product]) flash[:notice] = ''Product was successfully updated.'' redirect_to :action => ''listform'' else render :action => ''edit'' end end count=count.to_i-1 end =========================================Product.rb=================class Product < ActiveRecord::Base validates_presence_of :name , :demand end ================================== But when I click ''add to cart'' button on my form I get error as:- ======================================================undefined method `product_id2='' for #<Product:0x3c98b30> ====================================================== Why this error is coming? Please tell me. Thanx in advance. Prash -- Posted via http://www.ruby-forum.com/.
Agnieszka Figiel
2006-Mar-17 12:53 UTC
[Rails] Re: How to update values in database in this form?
Prashant Tiwari wrote:> Hi, > This is my rHtml page & Corresponding actions & model pages:- > ===listform.rHtml================> <h1>Products List</h1> > <%= start_form_tag(:action => "add_to_cart")%> > > <table> > <tr> > <% > i=0 > %> > <th> > Product No. > </th> > <th> > Unit Price > </th> > <th> > Available Qty. > </th> > <th> > Demand Qty. > </th> > </tr> > <% for product in @products %> > <tr> > <td> > <%=hidden_field ''product'',''product_id''+i.to_s, :value=>product.id%> > <%=product.name%> > </td> > <td> > <%=product.unit_price%> > </td> > <td> > <%=product.quantity%> > </td> > <td> > <%=text_field("product", "dem_quantity"+i.to_s, "size" => 20)%> > </td> > </tr> > <% > i=i+1 > %> > <% end%> > <tr> > <%=hidden_field ''product'',''count'', :value=>i.to_i-1%></td> > <td colspan=4 align="center"> > <%= submit_tag("Add to Cart") %> > </td> > </tr> > </table> > <%= end_form_tag %> > =================================> ======products_controller.rb======> def add_to_cart > count=params[:product][:count] > while count.to_i>=0.to_i > @product = Product.find(params[:product][''product_id''+count.to_s]) > print "pppp" + params[:product][''dem_quantity''+count.to_s] > if params[:product][''dem_quantity''+count.to_s]=="" > print @product.name > else > if @product.update_attributes(params[:product]) > flash[:notice] = ''Product was successfully updated.'' > redirect_to :action => ''listform'' > else > render :action => ''edit'' > end > end > count=count.to_i-1 > end > ==================================> =======Product.rb=================> class Product < ActiveRecord::Base > validates_presence_of :name , :demand > end > ==================================> > But when I click ''add to cart'' button on my form I get error as:- > ======================================================> undefined method `product_id2='' for #<Product:0x3c98b30> > ======================================================> > Why this error is coming? > > Please tell me. > Thanx in advance. > PrashHi, I think this is because of "@product.update_attributes(params[:product])" - your params contain field labels like "product_id2" which are not fields in your model, so you cannot update it like that. You coul use "update_attributes" passing the hash with correct attribute names and values from your params. Also, have a look at the "index" option in form helpers, this might help you make your code more tidy - from the api: ===If the helper is being used to generate a repetitive sequence of similar form elements, for example in a partial used by render_collection_of_partials, the "index" option may come in handy. Example: <%= text_field "person", "name", "index" => 1 %> becomes <input type="text" id="person_1_name" name="person[1][name]" value="<%= @person.name %>" /> === -- Agnieszka -- Posted via http://www.ruby-forum.com/.