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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060414/630ce595/attachment.html
Mickael Faivre-Macon
2006-Apr-17 18:53 UTC
[Rails] Updateform with a has_many relationship ?
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:> > 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.
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.nlAre 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. -- Posted via http://www.ruby-forum.com/.
Mickael Faivre-Macon
2006-Apr-17 20:20 UTC
[Rails] Re: Updateform with a has_many relationship ?
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. On 4/17/06, Bryan Duxbury <bryan.duxbury@gmail.com> wrote:> 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.