Understanding this Shopping Cart thing - rails4

17 views
Skip to first unread message

MatCouto

unread,
Feb 18, 2014, 5:56:13 AM2/18/14
to rubyonra...@googlegroups.com
Hey folks,
I was wondering what would be the best approach for this situation.
In my application layout I've got a partial which shows the user's cart if there is any.
- if @cart && !@cart.line_items.empty?
  %h1 Your Cart
  = render @cart
 
However, if I want to, for instance, click on the button to show me this particular product details (/product/22), a NilException is thrown as I would've lost @cart object on the request.
I managed to fix it by adding this piece of code to the product's controller:

before_action :load_current_cart, only: [:show]
 
def load_current_cart
  @cart = current_cart
end

Is there a better/clean way to do it?
Thanks in advance

Frederick Cheung

unread,
Feb 18, 2014, 6:16:13 AM2/18/14
to rubyonra...@googlegroups.com
I've been using http://cells.rubyforge.org for this sort of stuff - keeps that sort of data thing where closer to the thing that actually uses it rather than it being scattered across all the controllers that might need it.

Fred 
Reply all
Reply to author
Forward
0 new messages