I need to do shoping cart, but why when i first go to page, i didn’t see @cart object, but if refresh, all is ok. If simple say: cart is created not on first page load, but on second, and this is bad…. How to do, that when i open in browser url of page, i imidiatly see cart object?
My code: (app_controller)
before_filter :current_cart
private
def current_cart
Cart.find(session[:cart_id])
@cart = Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
cart = Cart.create
session[:cart_id] = cart.id
cart
end
And view:
%li
= link_to "Перейти в корзину", @cart
But how to create cart object when page is open…. not when i am on page, an done something….
Try this: