How do I assign the cart to the logged in customer?
you can do in different ways,
1. Like what you are trying to implement, Create a new Card Object,
once a user logs in and assign that card_id to
the logged in user's session. Then, from then onwards use that card id
to add any product which plan to purchase.
2. Create Some database relation-ship between user and card model.
3. Use javascript, ajax and cookie to implement your card logic.
OK, so I have the following tables in my db: customers, carts,
cart_items.
The carts table has 2 columns: id and customer_id
The Customer class has: has_many : carts
The Cart class has: has_many : products :through :cart_items
has_one :customer
Also, when the customer logs in, to display his name for example, I
use @c.name in the view. I'm really new to rails but from my
understanding, @c is an array (is that right?) that holds all of the
customer's details.
I've tried to assign the @cart to @c but it didn't work. I'm not sure
how to do this.
Also now, I call all the cart items by using @cart.products.
How do i cahnge it to call for all the products in the cart that
belongs to customer?
This is the code for customer login and logout:
def login
# examine the form data for "name" and "password"
pw,name = params[:customer].values_at(*%w{password name})
# Retrieve the customer record for the name and store it in a
variable 'c'
c = Customer.find_by_name(name)
# if such record exists, and it's password matches the password
from the form
if c && Digest::SHA1.hexdigest(pw) == c.password
# start a session with the customer id
@session['customer'] = c.id
if @cart.products.empty?
redirect_to :controller => "catalog"
else
redirect_to :action => "view_cart"
end
else
# otherwise report an error
flash[:notice] = "Invalid Login"
redirect_to :controller => "catalog"
end
end
def logout
@session['customer'] = nil
redirect_to :controller => "catalog"
end
How should I change my code to make this happen?
Thanks,
Elle