Updateform with a has_many relationship?

Hello all,

I would like to be able to update the “amount” of a certain item in
me order list.
The problem I run into is how to define the text_field element.

@order is the current order, which has_many :order_item which in turn
belongs_to :order.

order_item has a field “amount” which should be modified…

What I tried is this below, but I get a undefined method
`order_item.amount’ for #Order:0x2375764

<%total_price=0%>
<[email protected]_item.each{|item|%>
<tr class=<%=cycle(‘even’,‘odd’)%>>
<%=item.product.name%>
<%=text_field ‘order’,‘order_item.amount’,:size=>3%>
<%=item.product.price%>
<%=(item.product.priceitem.amount)%>
<%=image_tag ‘delete’%>
<%total_price+=(item.product.price
item.amount)%>

<%}%>

With kind regards,

Gerard de Brieder.
Govannon.
web : http://www.govannon.nl
email : [email protected]

Hi all,

This is certainly a basic one, but I have the same question.

Mickael.

On 4/14/06, Gerard de Brieder [email protected] wrote:

order_item has a field “amount” which should be modified…

<%=(item.product.price*item.amount)%> <%=image_tag 'delete'%> <%total_price+=(item.product.price*item.amount)%> <%}%>

Gerard de Brieder wrote:

Hello all,

I would like to be able to update the “amount” of a certain item in
me order list.
The problem I run into is how to define the text_field element.

@order is the current order, which has_many :order_item which in turn
belongs_to :order.

order_item has a field “amount” which should be modified…

What I tried is this below, but I get a undefined method
`order_item.amount’ for #Order:0x2375764

<%total_price=0%>
<[email protected]_item.each{|item|%>

> <%=item.product.name%> <%=text_field 'order','order_item.amount',:size=>3%> <%=item.product.price%> <%=(item.product.price*item.amount)%> <%=image_tag 'delete'%> <%total_price+=(item.product.price*item.amount)%> <%}%>

With kind regards,

Gerard de Brieder.
Govannon.
web : http://www.govannon.nl
email : [email protected]

Are you trying to save the total amount of the order to the order record
when you submit? If so, then, you should do this:

<% order.amount = 0 %>
<%= start_form_tag … %>
<% for order_item in @order.order_items %>
<% your display logic here %>
<% order.amount += order_item.item.price * order_item.amount %>
<% end %>
<% hidden_field :order, :amount %>
<%= end_form_tag %>

This is untested, but I think it should work. Let me know how it works
out.

Hi Bryan,

I think I can reply for Gerard.
The problem really is in the ‘display logic’ you’ve summarized.

He gets a undefined method
`order_item.amount’ for #Order:0x2375764

here:

<%=item.product.name%>

Mickael.