0

Using code based upon an old Railscast, http://railscasts.com/episodes/142-paypal-notifications , I have been attempting to pass information about the cart to and from different parts in the site, the issue appears to be with the following code.

def create
@pProduct = Product.find(params[:product])
@item = Item.create!(:cart => current_cart, :product => @pProduct, :quantity => 1, :pPrice => @pProduct.pPrice)
flash[:notice] = "Successfully added #{@pProduct.pName}"
redirect_to current_cart_url 
end

And the code to handle this input, found in the app controller.

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

The produced error is.

ActiveModel::MissingAttributeError in ItemsController#create 
can't write unknown attribute `cart_id'
Rails.root: /home/timewaster/Aptana Studio 3 Workspace/cs2512
Application Trace | Framework Trace | Full Trace

app/controllers/items_controller.rb:4:in `create'

I know this is probably something ridiculously simple, but any input would be welcome as I've be slamming my head into a wall for the past half hour with this.

EDIT: As requested, routes.rb I apologise for how all over the place this one is.

http://pastebin.com/gq4Ekzvn EDIT: Turns out I'm an idiot, Schema issues suck.

  • would you mind posting the `routes.rb` file? seems like you may be missing something there? – mportiz08 Mar 16 '12 at 02:18
  • http://pastebin.com/gq4Ekzvn Pastebinned for ease. The current_cart method is actually in my application controller a fact I managed to neglect. – timewaster Mar 16 '12 at 02:27
  • i don't see anything in there that defines the current_cart_url as a route helper -- can you run `rake routes` to make sure that `current_cart_url` is defined? – mportiz08 Mar 16 '12 at 02:32
  • 1
    That one I'm aware of, the error is from a touch earlier and my tests had made me aware of it. Thanks though, I appreciate the headsup. – timewaster Mar 16 '12 at 02:42

1 Answers1

0
@item = Item.create!(:cart => current_cart, :product => @pProduct, :quantity => 1, :pPrice => @pProduct.pPrice)

should read

@item = Item.create!(:cart_id => @current_cart.id, :product_id => @pProduct.id, :quantity => 1, :pPrice => @pProduct.pPrice)

Assuming you have the correct associations defined in the model class for Item.

More specifically - this assumes that your Item belongs_to cart.

xyz
  • 1,513
  • 2
  • 13
  • 17
  • With that change nil is called and attempting to use @current_cart = current_cart causes the exact same error, for obvious reasons. What I want is the cart to be created if non exists or if the current cart id has been paid for. The assumption is correct, the model is definitely interacting correctly. Apologies if this was not clear, I'm running on a distinct lack of sleep. – timewaster Mar 16 '12 at 02:02
  • Can you post your models and/or your Item and Cart table schema please? The error suggests there's no cart_id column in your item table. – xyz Mar 16 '12 at 02:25
  • 1
    It looks to be a schema issue, 3AM coding lead to me missing a link inside of the damn table itself. Thank you, I never would have thought to have triple checked that one. – timewaster Mar 16 '12 at 02:37