0

Basically i've been following the RailsCasts Paypal Basic tutorial and after a cart is marked as purchased, you need to reset session[:cart_id] = nil

Here's my code

class Customer::CartsController < ApplicationController
 def show
   @cart = if current_user
    current_user.cart
   else
    Cart.find session[:cart_id]
    Cart.destroy session[:cart_id]
    session[:cart_id] = nil if current_user.cart.purchased_at
   end
   if session[:cart_id].nil?
    @cart = Cart.create(session[:cart_id])
   end
   @cart
   end
end

I can't seem to figure out why its not working. I would appreciate any pointers as to where i've gone wrong. Thanks

UPDATE: i figured when the 'if current_user' is true, which is most of the time. The else block doesn't get rendered. So i updated it to

 class Customer::CartsController < ApplicationController
   def show
     @cart = if current_user
     current_user.cart ||= Cart.find session[:cart_id]
     session[:cart_id] = nil if current_user.cart.purchased_at
     end
     if session[:cart_id].nil?
      current_user.cart = Cart.create(params[:id])
      session[:cart_id] = current_user.cart.id
     end
     current_user.cart
  end

end

Still no luck! No idea, what i'm doing wrong now....

1 Answers1

0

Hard to see without posting the actual error, but I see an issue in the line below:

current_user.cart = Cart.create(params[:id])

The function create takes a hash as the argument, so you would need to call it as shown below:

current_user.cart = Cart.create(:user_id => params[:id])

** Assuming that the id param is the user id.

Check the create function documentation.

aruanoc
  • 817
  • 1
  • 7
  • 9
  • The railscasts episode i was following actually did Cart.create! But when i follow that, it messes up the view of the cart. Throws up this error >> [link](https://www.dropbox.com/s/8snff4essml1gug/error.png?dl=0) – Rails_learner May 13 '16 at 18:17
  • The only difference between `create` and `create!` is that the first returns false if the object isn't saved to the DB, while the second actually raises an exception in that case. Looking at your error, `create!` should have raised an exception before getting there, so it looks like `@cart` gets set back to nil between `create!` and getting to the view. Are you using something like "byebug" to debug? If you debug step by step, you will find the issue. – aruanoc May 13 '16 at 19:37
  • So i changed the code to this > def show (at)cart = if current_user current_user.cart ||= Cart.find_by(session[:cart_id]) session[:cart_id] = nil if current_user.cart.purchased_at end if session[:cart_id].nil? current_user.cart = Cart.create!(user_id: params[:id]) session[:cart_id] = current_user.cart.id end (at)cart = current_user.cart end | It works but the cart doesn't reset when cart.purchased_at is true... – Rails_learner May 14 '16 at 12:45
  • This is the command to mark as purchased > def mark_cart_as_purchased cart = Cart.find_by(params[:user_id]) if status == "Completed" cart.update_attribute(:purchased_at, Time.now) end end – Rails_learner May 14 '16 at 12:45