Ruby on Rails Tuesday, February 18, 2014

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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/fafccfa9-c44c-49ef-8f29-68843d604fe0%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment