I have an App with Demos and Ratings. The basic idea is that each user can rate demos, and they can update the ratings. However, I'm having a lot of errors with several things:
Updating the rating for the demo
Creating a form to update the rating
Demo Controller
def show
#demo = Demo.find(params[:id])
#user = User.find(#demo.user_id)
#topic = Subject.find(#demo.subject_id)
if repeat_reviewer?(current_user)
#rating = Rating.create(reviewer: current_user, reviewed: #demo)
else
#rating = Rating.where(reviewer: current_user, reviewed: #demo)
end
end
....
def repeat_reviewer?(user)
#demo = Demo.find(params[:id])
return false unless user == User.where(reviewed: #demo)
end
def update_average_rating
#value = 0
self.ratings.each do |r|
#rating += #rating.value
end
#total = self.ratings.size
update_attributes(rating_average: #value.to_f / #total.to_f)
end
Ratings Controller
before_action :require_user, only: [:create, :update]
after_action :update_demo_rating
def create
#rating = Rating.create(rating_params)
if #rating.save
redirect_back_or
end
end
def update
#rating = Rating.where(reviewer: current_user)
if #rating.update_attributes(rating_params)
redirect_back_or
end
end
private
def rating_params
params.require(:rating).permit(:value, :reviewer_id, :reviewed_id)
end
def update_demo_rating
#rating = Rating.find(params[:id])
#demo = #rating.reviewed
#demo.update_average_rating
end
Demos Show View
<%= form_for #rating, url: ratings_path(#rating), method: :put do |f| %>
<%= options_for_select([[5,5],[3,3],[1,1]], 1) %>
<%= f.submit %>
I have errors with generating the form, with updating the correct attributes, and also find the rating associated with the demo and user.
Related
I want to create a ruby app to check if a server status is up or down.
Later I will find a way to adapt my json path to find a way in the diverse syntax. But for now, I'm stuck with a dirty "undefined method `each' for nil:NilClass"
Ping controller:
def index
#pings = Ping.all
end
def new
#service = Ping.new
end
def create
#ping = Ping.new(ping_params)
#ping.service = #service
#ping.up = test_ping
#ping.save
end
def test_ping
require 'json'
require 'open-uri'
url = 'https://www.facebook.com/platform/api-status/'
fb_status_serialized = open(url).read
fb_status = JSON.parse(fb_status_serialized)
if fb_status['current']['health'] == 1
test_ping = true
else
test_ping = false
end
end
private
def ping_params
params.require(:ping).permit(:service_id, :up)
end
def set_ping
#ping = Ping.find(params[:ping_id])
end
end
Service controller:
(here are my setup for the services I want to add)
class ServiceController < ApplicationController
before_action :set_service, only: [:edit, :show, :destroy]
def index
#services = Service.all
end
def new
#service = Service.new
end
def edit
end
def show
end
def create
#service = Service.new(service_params)
#service.save
if #service.save
redirect_to service_path(#service)
else
render :new
end
end
def destroy
#service.destroy
#service.destroy
redirect_to services_path
end
private
def set_service
#service = Service.find(params[:id])
end
def service_params
params.require(:service).permit(:name, :web_api, :json_path)
end
end
View (service index):
<%= #services.each do | s | %>
<p><%= s.name %></p>
<p><%= s.web_api %></p>
<p><%= s.json_path %></p>
<p><%= s.id %></p>
<%= #pings.each do | p | %>
<%# if p.service_id == s.id %>
<!-- undefined -->
<% end %>
<p>|||</p> <%= Ping.new(up: true, service: s ) %> <p>|||</p>
<% end %>
You're trying to iterate over #pings that are inside the #services iteration, but this hasn't been defined, you've defined only services, and the iteration in pings won't work if the object in which each is being applied has a nil value.
Whatever is the controller you're using to render the view, you need to define both instance variables, try with:
def index
#services = Service.all
#pings = Ping.all
end
On my site users can both take and retake a quiz, meaning I will use the quiz code in a partial view that will be used both by the the edit and new views.
To render my partials I have in my new view:
<%= render partial: "quiz", locals: { url: quiz_bs_path } %>
and in my edit view:
<%= render partial: "quiz", locals: { url: edit_quiz_b_path } %>
The link_to locations are from my rake routes results:
quiz_bs GET /quiz_bs(.:format) quiz_bs#index
POST /quiz_bs(.:format) quiz_bs#create
new_quiz_b GET /quiz_bs/new(.:format) quiz_bs#new
edit_quiz_b GET /quiz_bs/:id/edit(.:format) quiz_bs#edit
quiz_b GET /quiz_bs/:id(.:format) quiz_bs#show
PATCH /quiz_bs/:id(.:format) quiz_bs#update
PUT /quiz_bs/:id(.:format) quiz_bs#update
DELETE /quiz_bs/:id(.:format) quiz_bs#destroy
I am getting an Argument Error in my edit view saying First argument in form cannot contain nil or be empty. The line on which the error is being called is in my partial:
<%= form_for #quiz_bs, url: url, method: :post do |f| %>
My controller code is:
class QuizBsController < ApplicationController
before_action :require_sign_in
def show
#quiz_bs = QuizBs.find(params[:id])
end
def new
#quiz_bs = current_user.quiz_bs || current_user.build_quiz_bs
end
def create
#quiz_bs = QuizBs.new
#quiz_bs.bs01 = params[:quiz_bs][:bs01]
#quiz_bs.bs02 = params[:quiz_bs][:bs02]
#quiz_bs.bs03 = params[:quiz_bs][:bs03]
#quiz_bs.bs04 = params[:quiz_bs][:bs04]
#quiz_bs.bs05 = params[:quiz_bs][:bs05]
#quiz_bs.bs06 = params[:quiz_bs][:bs06]
#quiz_bs.user = current_user
if #quiz_bs.save
flash[:notice] = "Quiz results saved successfully."
redirect_to user_path(current_user)
else
flash[:alert] = "Sorry, your quiz results failed to save."
redirect_to welcome_index_path
end
end
def update
#quiz_bs = QuizBs.find(params[:quiz_bs])
#quiz_bs.assign_attributes(quiz_bs_params)
if #quiz_bs.save
flash[:notice] = "Post was updated successfully."
redirect_to user_path(current_user)
else
flash.now[:alert] = "There was an error saving the post. Please try again."
redirect_to welcome_index_path
end
end
private
def quiz_bs_params
params.require(:quiz_bs).permit(:bs01, :bs02, :bs03, :bs04, :bs05, :bs06)
end
end
The model is:
class QuizBs < ActiveRecord::Base
before_save :set_bscode
def set_bscode
self.bscode = "#{self.bs01}#{self.bs02}#{self.bs03}-#{self.bs04}#{self.bs05}#{self.bs06}"
end
belongs_to :user
validates :user, presence: true
end
Any ideas where I am going wrong?
Keep it more simple, if you want to assign to current_user, create method will do for you.
Partial:
#new.html.erb
<%= render partial: "quiz", locals: {quiz: #quiz_bs, url: :quiz_bs } %>
#edit.html.erb
<%= render partial: "quiz", locals: {quiz: #quiz_bs, url: :quiz_b } %>
Form:
#_form.html.erb
<%= form_for quiz, url: url do |f| %>
Controller:
def new
#quiz_bs = QuisBs.new
end
You do not have edit action in controller, so rails assumes it being empty. Thus #quiz_bs is really nil
Add something like
def edit
#quiz_bs = QuizBs.find(params[:id])
end
also in your update action params[:quiz_bs] is most likely nil too and should be changed to params[:id]
It is not necessary to create RESTfull methods in controller but if you are using it as I can see #quiz_bs then you will have to initialize it first then you can use it, In your new view you initialize the #quiz_bs by
#quiz_bs = current_user.quiz_bs || current_user.build_quiz_bs
then you are using it.
and also routes which is generated for edit is needed an id i.e.
edit_quiz_b GET /quiz_bs/:id/edit(.:format) quiz_bs#edit
so you will have to pass #quiz_bs like
edit_quiz_b_path(#quiz_bs)
GOT it?
Some updates to your controller:
before_action :assign_quiz_bs, only: [:show, :edit, :update, :destroy]
#...
def update
#quiz_bs.update(quiz_bs_params)
# .... rendering ...
end
private
def assign_quiz_bs
#quiz_bs = Quiz.find(params[:id])
end
def quiz_bs_params
params.require(:quiz_bs).permit(:bs01, :bs02, :bs03, :bs04, :bs05, :bs06)
end
I have a form on a cart page to update line_items. So the resource I am updating needs to exit the cart controller and enter the line_items controller... I am using the following form...
<%= simple_form_for shifted_commerce_line_item_path(item),
:url => {controller: 'line_items', action: 'update'} do |f| %>
I get no route matches...
No route matches {:action=>"update", :controller=>"shifted_commerce/line_items"}
The thing is... I have the the action in the controller...
I'll post my routes.
shifted_commerce_line_items GET /shifted_commerce/line_items(.:format) shifted_commerce/line_items#index
POST /shifted_commerce/line_items(.:format) shifted_commerce/line_items#create
new_shifted_commerce_line_item GET /shifted_commerce/line_items/new(.:format) shifted_commerce/line_items#new
edit_shifted_commerce_line_item GET /shifted_commerce/line_items/:id/edit(.:format) shifted_commerce/line_items#edit
shifted_commerce_line_item GET /shifted_commerce/line_items/:id(.:format) shifted_commerce/line_items#show
PATCH /shifted_commerce/line_items/:id(.:format) shifted_commerce/line_items#update
PUT /shifted_commerce/line_items/:id(.:format) shifted_commerce/line_items#update
DELETE /shifted_commerce/line_items/:id(.:format) shifted_commerce/line_items#destroy
How do I get this form to hit the update action in my controller?
Don't we see the above route? both PUT and PATCH match /shifted_commerce/Line_items/:id...and I'm passing the id with the (item)...
Update 1 I'll also add that I'm running this form in a loop, so every line_item can be edited. #anything isn't what I'd want to pass to the path for an ID... also, I'm operating from the carts controller, in the show action. Not a line_item controller, edit action.
Update 2 If I do the following simple_form_for:
<%= simple_form_for shifted_commerce_line_item_path(item), :url => {controller: 'line_items', action: 'create'} do |f| %>
It successfully takes me into the create action of the line_items controller. But if I do
<%= simple_form_for shifted_commerce_line_item_path(item), :url => {controller: 'line_items', action: 'update'} do |f| %>
I get the route error. What's the deal?! Why does it work for create but not for update? I have the update action in my controller...
ShiftedCommerce::LineItemsController
class ShiftedCommerce::LineItemsController < ApplicationController
before_action :set_line_item, only: [:show, :edit, :update, :destroy]
before_action :set_line_item_item, only: [:show, :edit, :update, :destroy]
# GET /line_items
# GET /line_items.json
def index
#line_items = ShiftedCommerce::LineItem.all
end
# GET /line_items/1
# GET /line_items/1.json
def show
end
# GET /line_items/new
def new
#line_item = ShiftedCommerce::LineItem.new
end
# GET /line_items/1/edit
def edit
end
# POST /line_items
# POST /line_items.json
def create
#cart = current_cart
# item is built from base_item, after finding associated product
#base_item_id = params[:shifted_commerce_line_item][:base_item_id]
get_item_id_from_base_item_params
build_line_item
## Does a line item with the same itemt_id already exist in cart?
if #line_item.exists_in_collect?(current_cart.line_items)
#if so, change quantity, check if there's enough stock
if current_cart.where_line_item_with(#item_id).update_quantity(#line_item.quantity) == true
#line_item.destroy
redirect_to shifted_commerce_base_items_path
flash[:success] = "Detected item In Cart, Added Your Amount More to Quantity"
else #otherwise there's not enough product in stock.
redirect_to shifted_commerce_cart_path
flash[:failure] = "Cannot Add To Cart, Not Enough In Stock"
end
else # This product isn't in the cart already let's save it!
if #line_item.have_enough_item? == true # if there is enough stock, save
if #line_item.save
respond_to do |format|
format.html { redirect_to shifted_commerce_base_items_path,
:notice => "Added to Cart." }
format.xml { render :xml => #line_item,
:status => :created, :location => #line_item }
end
else
format.xml { render :xml => #line_item.errors,
:status => :unprocessable_entity }
end
else # not enough stock, not saved.
redirect_to shifted_commerce_base_items_path
if #line_item.item.stock_qty > 0
flash[:failure] = "Sorry! We Only Don't Have Enough In Stock"
else
flash[:failure] = "Sorry! That Item Is Out Stock"
end
end
end
end
# PATCH/PUT /line_items/1
# PATCH/PUT /line_items/1.json
def update
#line_item = ShiftedCommerce::LineItem.find(params[:id])
raise
#line_item.attributes = line_item_params
if #line_item.over_order_cap? #is the quantity over maximum?
flash[:error] = "Contact Us For Orders of This Size -- Quantity Set To Max"
redirect_to cart_path
else # quantity to change to not over maximum.
if #line_item.have_enough_item? == true
#line_item.update(line_item_params)
#did they update quantity to 0?
if #line_item.quantity <= 0
#line_item.destroy
flash[:success] = "Item Removed"
redirect_to :back
else
flash[:success] = "Itemed Updated"
redirect_to cart_path
end
else
redirect_to cart_path
flash[:failure] = "We Don't Have Enough In Stock. Update Failed"
end
end
end
# DELETE /line_items/1
# DELETE /line_items/1.json
def destroy
#line_item.destroy
respond_to do |format|
format.html { redirect_to line_items_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_line_item
#line_item = LineItem.find(params[:id])
end
def set_line_item_item
#line_item_name = #line_item.item.base_item
end
# Never trust parameters from the scary internet, only allow the white list through.
def line_item_params
params.fetch(:shifted_commerce_line_item, {}).permit(:item_id, :cart_id, :order_id, :quantity, :weight, :units)
end
def build_line_item
#line_item = #cart.line_items.build(
:item_id => #item_id,
:order_id => nil,
:weight => params[:shifted_commerce_line_item][:weight],
:quantity => params[:shifted_commerce_line_item][:quantity]
)
end
def get_item_id_from_base_item_params
#item_id = ShiftedCommerce::Item.where(:base_item_id => #base_item_id).where(:size_id => params[:shifted_commerce_line_item][:size]).first.id
end
end
line_item.rb
class ShiftedCommerce::LineItem < ActiveRecord::Base
belongs_to :item
belongs_to :cart
after_create :set_order_weight#, :set_package_dimentions
after_update :set_order_weight#, :set_package_dimentions
#max capactiy here
def have_enough_item?
if self.item.stock_qty >= self.quantity
return true
else
if self.quantity_was == nil
return false
else
self.quantity = self.quantity_was
self.save
return false
end
end
end
def set_order_weight
if
self.cart.nil?
else
self.cart.total_weight = self.cart.line_items.to_a.sum {|item| (item.weight)*(item.quantity)}
self.cart.save
end
end
def over_order_cap?
if self.quantity > 5
self.quantity = 5
self.save
return true
end
return false
end
def update_quantity(qty)
self.quantity += qty
if self.have_enough_item? == true
self.save
end
end
def exists_in_collect?(items)
items.each do |item|
return true if self.item_id == item.item_id
end
return false
end
def set_order_total_units
if
self.cart.nil?
else
self.set_cart_units
self.cart.total_units = self.cart.total_units
self.cart.save
end
end
def total_price
item.base_item.price * quantity
end
end
Routes.rb
namespace :shifted_commerce do
resources :line_items
resources :items
resources :base_items
resource :cart, only: [:show, :update, :destroy] do
resource :order, only: [:show, :create, :update, :edit, :new]
end
end
Change this and try:
<%= simple_form_for item, url: shifted_commerce_line_items_path(item), method: :put do |f| %>
as per you routes
Update:
Sorry it must be post but not put like this:
<%= simple_form_for item, url: shifted_commerce_line_item_path(item), method: :post do |f| %>
Try using <%= simple_form_for ([:shifted_commerce, item]) do |f| %> or
<%= simple_form_for item, :url => shifted_commerce_line_item_path do |f| %>
I can't solve this problem.
When I try to use "Chatroom#new" method, I I got this error, ActionController::ParameterMissing param is missing or the value is empty .
below codes are the ChatroomController.
class ChatroomsController < ApplicationController
before_action :find_room_owner,only:[:index]
before_action :objects_for_index,only:[:index]
def index
#/users/:user_id/cart/items/chatroom
sign_in #user if signed_in?
if #seller && #buyer
flash[:success] = "U are owners of the chatroom"
#messages = Message.all #Messageのmodelを作成
else
flash[:error] = "U cant enter the chatroom."
redirect_to user_cart_items_url(#user,#cart,#items) ##user,#cart,#itemsをgetしろ
end
end
def new
#user = User.find(params[:user_id])
#cart = Cart.find(params[:user_id])
#item = Item.find(params[:item_id])
#message = Message.new(message_params)
end
def create
#user = User.find(params[:user_id])
#message = #user.messages.build
if #message.save
#message.update_attributes(user_id:#user.id)
redirect_to user_cart_chatroom_path(#user,#cart,#items)
else
flash[:error] = "could not create any message."
render 'new'
end
end
private
def message_params
params.require(:messages).permit(:id,:user_id,:messages)
#params{:message => :id,:user_id,:messages}
end
#before_aciton
def find_room_owner
#item = Item.find(params[:item_id])
#buyer = User.find(params[:user_id])
#seller = Product.find_by(user_id:#item.user_id)
end
def objects_for_index
#user = User.find(params[:user_id])
#items = Item.all
end
end
Below codes are the view of Message#new.
<h1>Chatroom#new</h1>
<p>Find me in app/views/chatroom/new.html.erb</p>
<%= form_for #message,url:new_user_cart_item_chatroom_path(#user,#cart,#item) do |f| %>
<%= f.label :messages %>
<%= f.text_field :messages %>
<%= f.submit "new message", class:"btn btn-default" %>
<% end %>
Below codes are the migration of Message.
class CreateMessages < ActiveRecord::Migration
def change
create_table :messages do |t|
t.integer :user_id
t.string :messages
t.timestamps
end
end
end
Below codes are the model of message.
class Message < ActiveRecord::Base
validates :messages,presence:true,length:{maximum:200}
belongs_to :user
end
Below codes are the routes.
KaguShop::Application.routes.draw do
resources :users,only:[:show,:new,:create,:edit,:update,:destroy] do
collection do
get 'get_images',as:'get_images'
end
resources :products,only:[:show,:index,:new,:create,:edit,:update,:destroy] do
collection do
post 'add_item', as:'add_item'
get 'get_image',as:'get_image'
get 'get_images',as:'get_images'
end
end
end
resources :users,only:[:show,:new,:create,:edit,:update,:destroy] do
resource :cart,except:[:new,:show,:edit,:destroy,:update,:create] do
collection do
post 'purchase', as:'purchase'
#get 'show' , as: 'show'
post 'create' , as: 'create'
#多分routeにas的な感じでtemplateを提供するのが一番いい気がする
delete 'destroy',as:'destroy'
delete 'remove_item',as:'remove_item'
end
resources :items,only:[:show,:index] do
collection do
get 'get_images',as:'get_images'
end
resources :chatrooms,only:[:index,:create,:new] do
end
end
end
end
resources :sessions,only:[:new,:create,:destroy]
root 'products#index'
match '/signup', to:'users#new',via:'get'
match '/signin', to:'sessions#new', via:'get'
match '/signout', to:'sessions#destroy', via:'delete'
match '/contact', to:'nomal_pages#contact', via:'get'
end
You should call Message.new without params because message_params nil in this request and this raise ActionController::ParameterMissing:
class ChatroomsController < ApplicationController
#.........
def new
#user = User.find(params[:user_id])
#cart = Cart.find(params[:user_id])
#item = Item.find(params[:item_id])
#message = Message.new
end
#........
private
def message_params
params.require(:messages).permit(:id,:user_id,:messages)
end
#.......
end
Two objects at the same time not one add to my shopping cart. Here is the controller and models :
class PublicController < ApplicationController
before_filter :find_or_create_cart, :only => [:add_to_cart, :show_cart]
def list
#products = Product.sorted.paginate(:per_page => 5 , :page => params[:page])
end
def add_to_cart
product = Product.find(params[:id])
#cart.add_product(product)
redirect_to(:action => 'show_cart')
end
def show_cart
end
private
def find_or_create_cart
#cart=session[:cart]||=Cart.new
end
end
AND the model :
class Cart
attr_reader :items
attr_reader :total_price
def initialize
#items = []
#total_price = 0.0
end
def add_product(product)
#items << LineItem.new_based_on(product)
#total_price += product.price
end
end
Model which come from a join table :
class LineItem < ActiveRecord::Base
belongs_to :order
belongs_to :product
def self.new_based_on(product)
line_item = self.new
line_item.product = product
line_item.quantity = 1
line_item.price = product.price
return line_item
end
end
It is a small application which is supposed to do online shopping, but when I hit the button add to cart this will add two objects (same object) to my shopping cart. Hope someone can help me.
I'll write another answer to address your code, but I felt this might help you.
We've developed our own cart system for one of our apps (you can see at http://firststopcosmeticshop.co.uk) we developed based on Ryan Bates' session models Railscast
How It Works
When using sessions to store cart values, you're basically storing a session var with id's from added cart items. Because you want to keep the session as thin as possible, you'll literally just have an array constructed of product id's:
session[:cart][:products] = [] # -> to init
session[:cart][:products] << product.id # -> builds array like [1,5,6,1,6,4,1,5]
This will allow you to then process the cart as you desire (by reading / manipulating the session[:cart][:products] array). If you test the app I referenced, it should help you see it in action
I mentioned your code is inefficient because you're relying on 3 data sources: LineItem, Cart and PublicController. What you really need is CartController and CartSession for a session-based model. This will allow you to call LineItem from your CartController, making it lean
Code
Our session based cart works like this:
#app/models/cart_session.rb
class CartSession
#Initalize Cart Session
def initialize(session)
#session = session
#session[:cart] ||= {}
end
#Cart Count
def cart_count
if (#session[:cart][:products] && #session[:cart][:products] != {})
#session[:cart][:products].count
else
0
end
end
#Cart Contents
def cart_contents
products = #session[:cart][:products]
if (products && products != {})
#Determine Quantities
quantities = Hash[products.uniq.map {|i| [i, products.count(i)]}]
#Get products from DB
products_array = Product.find(products.uniq)
#Create Qty Array
products_new = {}
products_array.each{
|a| products_new[a] = {"qty" => quantities[a.id.to_s]}
}
#Output appended
return products_new
end
end
#Qty & Price Count
def subtotal
products = cart_contents
#Get subtotal of the cart items
subtotal = 0
unless products.blank?
products.each do |a|
subtotal += (a[0]["price"].to_f * a[1]["qty"].to_f)
end
end
return subtotal
end
end
#app/controllers/cart_controller.rb
class CartController < ApplicationController
include ApplicationHelper
#Index
def index
#items = cart_session.cart_contents
#shipping = Shipping.all
end
#Add
def add
session[:cart] ||={}
products = session[:cart][:products]
#If exists, add new, else create new variable
if (products && products != {})
session[:cart][:products] << params[:id]
else
session[:cart][:products] = Array(params[:id])
end
#Handle the request
respond_to do |format|
format.json { render json: cart_session.build_json }
format.html { redirect_to cart_index_path }
end
end
#Delete
def delete
session[:cart] ||={}
products = session[:cart][:products]
id = params[:id]
all = params[:all]
#Is ID present?
unless id.blank?
unless all.blank?
products.delete(params['id'])
else
products.delete_at(products.index(id) || products.length)
end
else
products.delete
end
#Handle the request
respond_to do |format|
format.json { render json: cart_session.build_json }
format.html { redirect_to cart_index_path }
end
end
end
#config/routes.rb
get 'cart' => 'cart#index', :as => 'cart_index'
post 'cart/add/:id' => 'cart#add', :as => 'cart_add'
delete 'cart/remove(/:id(/:all))' => 'cart#delete', :as => 'cart_delete'
This basically allows you to keep a list of people's cart items in a simple session array. The array then gives you the ability to determine the quantity, product type, brand etc, all from the array. It's the most efficient way, and is what I'd recommend for your app
To address your question directly, I believe you need to do the following:
Routes
#config/routes.rb
resources :cart, only: :index do
post :add # -> allows you to add an item
end
Views
#app/views/cart/index.html.erb
<% for product in #products do %>
<%= product.name %>
<%= product.price %>
<% end %>
Controller
class CartController < ApplicationController
respond_to :html, :json, :js
before_action :cart_init, :only => [:add, :index]
#index should replace show_cart
def index
unless #cart.nil?
#products = Product.find #cart #-> will allow you to find multiple ID's
end
respond_with #products
end
#KISS (Keep It Simple)
def add
#cart << params[:id]
respond_with #cart
end
private
def cart_init
#cart = session[:cart][:products] ||= Cart.new #-> keep products in its own key. Allows you to add [:cart][:shipping] etc
end
end
Model
class Cart
attr_reader :items
attr_reader :total
def initialize
#items = session[:cart][:products] ||= []
#total = 0.0
end
end