- I'm building an store app that include a product catalog, details
page, and cart.
- I'm attempting to put them in the same layout using partials.
- Product catalog is the index action of the store controller
- I can get the cart on the page by adding @cart = find_cart to the
"def index" block. If there's not a cart already, find_cart creates one
I can't get the details view to be on the index page layout. I need
@product for the details view but I can't just add "@product =
Product.find(params[:id]) to the "def index" block because the I'll get
a RecordNotFound as there's not a product selected intially.
What should I do to get around this problem?
And while I'm at it, whats a better way to say "the 'def index' block"?
I'm not sure I 100% understand the problem, but here is a couple of
solutions
Conditionally check if you have params[:id] set before calling
Product.find to avoid generating the error?
@product= Product.find(params[:id]) unless params[:id].nil?
Or do a find(:all) so it returns an empty result set, that opens you up
to having multiple products returned though which you probably wouldn't
want:
@products = Product.find(:all, :conditions=>['id=?,params[:id]])
As for def index, maybe just the index action? Not sure what the common
practice is here myself.
Ok, that makes sense and what I was actually thinking initally, but it
doesn't really help me because of this:
Clicking on the product in the catalog invokes the "view_details"
action, which is fine if it not a partial (i.e. path in the browser is
/store/view_details/5). The problem is, according to my understanding
so far of partials, the action in "view_details" must "redirect_to
:action => :index". Because of that redirect, it appears as though the
selection doesn't persist and therefore my "view_details" (i.e. the
currently selected item) never displays.
So I'm starting to think that I have to store the current product
selected in the session by way of something like a "Basket" (more
temporary than a cart? good names are welcome) model.
Does that makes sense? Or is there a better way to make my selection
persist so I can display the catalog, deatails and cart all on the same
page?