Forum: Ruby on Rails How to update values in database in this form?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8c70506408fddb9740b4de7339055c77?d=identicon&s=25 Prashant Tiwari (tiwaripk)
on 2006-03-17 12:53
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
5b132632f47e8c7e2a83157cef4649c8?d=identicon&s=25 Agnieszka Figiel (agnessa)
on 2006-03-17 13:53
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.
> 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

  <input type="text" id="person_1_name" name="person[1][name]"
value="<%= @person.name %>" />
====

--
Agnieszka
This topic is locked and can not be replied to.