Checking the value of a session variable

Hi Folks.

I’m implementing a paypal notification as per the Railscast here #142 PayPal Notifications - RailsCasts

This is the code that is run to view the shopping cart:

def current_cart
if session[:cart_id]
  @current_cart ||= Cart.find(session[:cart_id])
  session[:cart_id] = nil if @current_cart.purchased_at
end
if session[:cart_id].nil?
  @current_cart = Cart.create!
  session[:cart_id] = @current_cart.id
end
@current_cart
end

Everything seems to be working fine except this. I’m checking the session[:cart_id] in the navigation to see if I should show a “My cart” link like so:

<% unless (session[:cart_id].nil?) %>
<li>	<%= link_to 'My Cart', current_cart_path %>   </li>

It hides the link until a cart exists and should again hide that link once the purchase has been made but it is not. It seems to be setting the value back to nil although the link still shows.
Any idea how I should be checking the value of the session variable in the view or is my problem elsewhere?
Thanks.

It looks like you’re setting session[:cart_id] to nil when purchased_at is set, but then immediately creating a new cart (if statement checking for session[:cart_id] being nil, which you just set it to), so session[:cart_id] is never nil.

Of course! Thank you so much.