这些是我的 pruduct.rb、cart.eb 和 item.rb
class Product < ActiveRecord::Base
attr_accessible :category_id, :color, :description, :price, :size, :title, :product_type, :sub_category_id, :image_url
belongs_to :category
belongs_to :sub_category
has_many :items
end
购物车.rb
class Cart < ActiveRecord::Base
has_many :items, dependent: :destroy
end
项目.rb
class Item < ActiveRecord::Base
attr_accessible :cart_id, :product_id,:product
belongs_to :cart
belongs_to :product
end
项目控制器
class ItemController < ApplicationController
def create
@cart=current_cart
product=Product.find(params[:product_id])
@item=@cart.items.build(product: product)
redirect_to clothes_path
flash.now[:success]="Product successfully added to Cart"
end
end
现在在我看来,当我 想显示购物车内容时
<%= @cart.items.each do |item| %>
current_cart方法 _
def current_cart
cart=Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
cart=Cart.create
session[:cart_id]=cart.id
cart
end
它给了我这个错误
nil:NilClass 的未定义方法“items”
这里有什么问题?
我正在关注示例Agile Web Development with Rails书。