ruby on rails - how to add a variable within each loop -
ruby on rails - how to add a variable within each loop -
in code:
<% @offer2.each |offer|%> <% @menuname=menu.find_by_menu_item_name(offer.menuname_get)%> <% @line_item.each |line_item|%> <%@validprice=(@menuname.price)*(offer.disamountorpercentage)%> <%end%> <%end%> i new in rails.i want add together @validprice += @validprice it's not working within loop or outside loop.so how sum of variable.
you should declare @validprice= 0 on top of loop. not initialize repeatedly.
<% validprice = 0 %> <% @offer2.each |offer|%> <% @menuname=menu.find_by_menu_item_name(offer.menuname_get)%> <% @line_item.each |line_item|%> <% validprice += (@menuname.price)*(offer.disamountorpercentage) %> <%end%> <%end%> it improve if create model method summations, rather run in view pages.
ruby-on-rails ruby-on-rails-4
Comments
Post a Comment