Hello I want to make simple shopping cart.
I am a beginner in programming. So please help.
I have items with prices and i want to sum up them (quantity equals to one).
veiw
<h1>Summ:<%= @items.total %></h1>
model
class Item < ActiveRecord::Base
def total
summ = 0
@items.each do |item|
summ += item.price
end
end
end
What is wrong. I have this error NoMethodError in Items#index
Rails defines an
Enumerable::summethod that you can call. So your view can look like this:You could also add a
totalmethod to yourOrder(or whatever model you are using that has_many :items). Something along the lines of:And then your view: