How to update values in database in this form?

Hi,
This is my rHtml page & Corresponding actions & model pages:-
===listform.rHtml=================

Products List

<%= start_form_tag(:action => "add_to_cart")%> <% i=0 %> <% for product in @products %> <% i=i+1 %> <% end%> <%=hidden_field 'product','count', :value=>i.to_i-1%>
Product No. Unit Price Available Qty. Demand Qty.
<%=hidden_field 'product','product_id'+i.to_s, :value=>product.id%> <%=product.name%> <%=product.unit_price%> <%=product.quantity%> <%=text_field("product", "dem_quantity"+i.to_s, "size" => 20)%>
<%= submit_tag("Add to Cart") %>
<%= 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

Prashant T. wrote:

Hi,
This is my rHtml page & Corresponding actions & model pages:-
===listform.rHtml=================

Products List

<%= start_form_tag(:action => "add_to_cart")%> <% i=0 %> <% for product in @products %> <% i=i+1 %> <% end%> <%=hidden_field 'product','count', :value=>i.to_i-1%>
Product No. Unit Price Available Qty. Demand Qty.
<%=hidden_field 'product','product_id'+i.to_s, :value=>product.id%> <%=product.name%> <%=product.unit_price%> <%=product.quantity%> <%=text_field("product", "dem_quantity"+i.to_s, "size" => 20)%>
<%= submit_tag("Add to Cart") %>
<%= 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

Hi,

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


Agnieszka