Yeah - i’ve got through the partials section the new depot app. It
doesn’t address the issue i had but generally i’m loving the updates to
the 2nd edition so far - worth the re-investment :0)
The problem (with regards to the issue i had) with the example in the
depot app, is that it doesn’t seem to explain how logic code can be used
in partials (not a problem for the example obviously, but it’s a
sticking point if you want to do anything more complex).
For example:
You have a cart display on the cart page and the same thing repeated on
the order confirm page. This was in the 1st Agile book implemented
using a component, but is not in the 2nd edition.
In the 2nd edition, a cart preview is implemented using a partial:
— views/store/index
<%= render :partial => ‘cart’, :object => @cart %>
— controllers/store_controller
def index
@cart = session[:cart]
end
— views/store/_cart.rhtml
<% @cart.items.each do |item| %>
This is fine for this example. But if you try and do the ‘cart
displayed on order confirm’ i mentioned above, it gets a little messy:
— views/checkout/confirm_order.rhtml / views/cart/view.rhtml
<%= render :partial => ‘cart/cart’ %>
— views/basket/_cart.rhtml
<% @cart.items.each do |item| %>
In order for this to work, we would need to pass the @cart object to the
partial in each respective view, like this:
— views/checkout/confirm_order.rhtml / views/cart/view.rhtml
<%= render :partial => ‘cart/cart’, :object => @cart %>
In order for @cart to be available in the views, it would need to be
assigned in both the CartController::view and
CheckoutController::confirm_order actions - this seems at risk of
repetition.
I’ve implemented it by creating a helper method that the partial uses
(mentioned in my last post) - but that seems a bit un-rails-y and i
found it had an issue when i tried to assign something from the session.
I guess you could also create a method that the controllers could use
globally to assign this kind of thing:
— cart controller
def view
get_cart_view
end
— checkout controller
def confirm_order
get_cart_view
other stuff
end
— application controller
def get_cart_view
@cart = session[:cart]
end
… but again something about that isn’t right :0)
Man, that was a bit of a ramble - think i’ll have to write this all up
in a difinitive tutorial when i discover the ‘right’ way :0) I have a
slight feeling of ‘barking up the wrong tree’ though…
Cheers,
Steve