Updateform with a has_many relationship?


#1

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%>
<removed_email_address@domain.invalid_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.priceitem.amount)%> <%}%>

With kind regards,

Gerard de Brieder.
Govannon.
web : http://www.govannon.nl
email : removed_email_address@domain.invalid


#2

Hi all,

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

Mickael.

On 4/14/06, Gerard de Brieder removed_email_address@domain.invalid 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)%> <%}%>

#3

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%>
<removed_email_address@domain.invalid_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 : removed_email_address@domain.invalid

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.


#4

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.