Ruby on Rails: render — display menu, the recycle bin on every page of the site?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
4 views
Good evening! Interested in the following question how to add a shopping cart for an online store on each page of the website (shopping cart like in the book Agile Web Dev), I have 4 models — Category, Product, Cart, Post.

layout application.html.erb
<%= render :partial => 'categories/cart', :locals => {:cart => @cart}%>


partial _cart.html.erb
<% if @cart.blank? %>
<% else %>
<% for item in @cart.items %>
<%= item.quantity %> × <%=h item.title %><%= number_to_currency(item.price) %>
<% end %>
Total: <%= number_to_currency(@cart.total_price) %>
<%= button_to 'empty basket', :controller => 'categories', :action => 'empty_cart'%>
<% end %>


In this scenario the shopping cart is displayed only in the view controller Categories. Can't figure out how to get from the session session[:cart] or else somehow the necessary data.


And another question, how to display a menu on each page now like this:
<%= render :partial => 'posts/post', :collections => Post.all %>


Thanks for the replies.
by | 4 views

1 Answer

0 like 0 dislike
If you can't get the cart from the session, look carefully in the book, from which he took the basket method find_cart:
\r
def find_cart session[:cart] ||= Cart.new end 

If just the problem is that the shopping cart object was assigned to the variable @cart, all templates, then move the find_cart method in the file application_controller.rb and in this file, include this method in a before_filter. Then you have the cart is unloaded from the session variable @cart in all controllers.
p.s. Do not copy the HTML code directly, half the code got eaten by the parser.
by
110,608 questions
257,186 answers
0 comments
27,859 users