Alb
Alb

Reputation: 221

Couldn't find Order without an ID

I'm a rails rookie with a weak understanding of the backend, and a difficult time learning it. I'm trying to get product, order, cart, and line item models and controllers in sync for a "simple" ecommerce app. Running into the following issue, which my go-to "mentor" does not have an answer for (or is tired of my questions).

ActiveRecord::RecordNotFound in LineItemsController#create

Couldn't find Order without an ID

Extracted source (around line #7):

@cart = current_cart
@product = Product.find(params[:product_id])
@order = Order.find(session[:order_id])

line_item = @order.line_items.build(line_item_attributes)

I'm not sure which other code is relevant enough to insert here. Please let me know what else I can provide.

I notice that some gentlemen on here enjoy snapping at young foolish rookies like myself; if thats you, hold back.

Thank you.

Upvotes: 0

Views: 1376

Answers (2)

infused
infused

Reputation: 24337

I think you need to use params[:order_id] instead of session[:order_id]. Isn't :order_id being passed in from a form?

Upvotes: 0

digijim
digijim

Reputation: 686

It would greatly benefit you to go through Michael Hartl's excellent Rails tutorial, which will make issues like this one seem trivial by the time you're through it.

But the bottom line is this: session[:order_id]'s value is nil - it isn't being defined before your LineItems' create action is called. Therefore, your @order variable isn't being populated by the line @order = Order.find(session[:order_id]). But trust me, you're going to run into far more complex issues than this one building a eCommerce app, so you really should give that tutorial a shot!

Upvotes: 1

Related Questions