Forum: Ruby on Rails Updateform with a has_many relationship ?

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.
912b664b56818fd36821d006577ff3aa?d=identicon&s=25 Gerard de Brieder (Guest)
on 2006-04-14 11:26
(Received via mailing list)
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%>
<%@order.order_item.each{|item|%>
	<tr class=<%=cycle('even','odd')%>>
	<td><%=item.product.name%></td>
	<td><%=text_field 'order','order_item.amount',:size=>3%></td>
	<td><%=item.product.price%></td>
	<td><%=(item.product.price*item.amount)%></td>
	<td><%=image_tag 'delete'%></td>
	<%total_price+=(item.product.price*item.amount)%>
</tr>
<%}%>


With kind regards,

Gerard de Brieder.
Govannon.
web : http://www.govannon.nl
email : info@govannon.nl
2f6686bd2c6bb633a1cbf7b5a73c8491?d=identicon&s=25 Mickael Faivre-Macon (micktaiwan)
on 2006-04-17 20:54
(Received via mailing list)
Hi all,

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

Mickael.


On 4/14/06, Gerard de Brieder <info@govannon.nl> wrote:
> order_item has a field "amount" which should be modified...
>  <td><%=(item.product.price*item.amount)%></td>
>  <td><%=image_tag 'delete'%></td>
>  <%total_price+=(item.product.price*item.amount)%>
> </tr>
> <%}%>
Fb23bc8cd4030c526b0689276b34c8bd?d=identicon&s=25 Bryan Duxbury (bryanduxbury)
on 2006-04-17 22:13
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%>
> <%@order.order_item.each{|item|%>
> 	<tr class=<%=cycle('even','odd')%>>
> 	<td><%=item.product.name%></td>
> 	<td><%=text_field 'order','order_item.amount',:size=>3%></td>
> 	<td><%=item.product.price%></td>
> 	<td><%=(item.product.price*item.amount)%></td>
> 	<td><%=image_tag 'delete'%></td>
> 	<%total_price+=(item.product.price*item.amount)%>
> </tr>
> <%}%>
>
>
> With kind regards,
>
> Gerard de Brieder.
> Govannon.
> web : http://www.govannon.nl
> email : info@govannon.nl

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.
2f6686bd2c6bb633a1cbf7b5a73c8491?d=identicon&s=25 Mickael Faivre-Macon (micktaiwan)
on 2006-04-17 22:22
(Received via mailing list)
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:
<td><%=item.product.name%></td>

Mickael.
This topic is locked and can not be replied to.