Hi again,

Every customer has a discount level assigned to them. when they add

items to the cart and on checkout (or when admin views all orders), I

would like to show the total after discount.

In the orders table, I insert the discount level at the time of order.

I ended up calculating the discounted total in about 3 or 4 of my

views and I thought that there should be a better way of doing this.

My logic tells me that the calculation should be done within the cart

class, where also the total is calculated (maybe my logic is wrong).

So, I added this to my cart.rb class:

def discounted_total

subtotal = @cart.total

discount = @customer.discount / 100

subtotal = (subtotal * discount)

@discounted = @cart.total - subtotal

end

But now I get this error:

NoMethodError in Cart#view_cart

Showing app/views/cart/view_cart.rhtml where line #31 raised:

You have a nil object when you didn’t expect it!

The error occurred while evaluating nil.total

Extracted source (around line #31):

31:

**$<%=**

two_dec(@cart.discounted_total) %>

two_dec(@cart.discounted_total) %>

app/models/cart.rb:38:in `discounted_total’

So, my question is:

How should I accomplish this? Any ideas how my code could be better?

And how do I get the order.rb see the same discounted total?

Thanks,

Elle