Ruby on rails 未定义的局部变量或方法“cart#u id';

Ruby on rails 未定义的局部变量或方法“cart#u id';,ruby-on-rails,cart,Ruby On Rails,Cart,我有一些问题,我使用《rails的敏捷web开发》一书,在任务D:购物车创建中,我发现了这个问题 背景信息。 控制器/应用程序\u controller.rb def current_cart Cart.find(session[:cart_id]) rescue ActiveRecord::RecordNotFound cart = Cart.create session[:cart_id]= cart_id cart end def create @cart = current_car

我有一些问题,我使用《rails的敏捷web开发》一书,在任务D:购物车创建中,我发现了这个问题

背景信息。
控制器/应用程序\u controller.rb

def current_cart
Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
cart = Cart.create
session[:cart_id]= cart_id
cart
end
def create


@cart = current_cart
product = Product.find(params[:product_id])
@line_item = @cart.line_items.build(:product_id => product.id )
respond_to do |format|
if @line_item.save
format.html { redirect_to @line_item.cart,
              notice: 'Line item was successfully created'}
format.json { render json: @line_item,
              status: :created, location: @line_item }
else
format.html { render action: "new" }
            format.json { render json: @line_item.errors,
               status: :unprocessable_entity }
   end
  end
end
控制器/行项目\u controller.rb

def current_cart
Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
cart = Cart.create
session[:cart_id]= cart_id
cart
end
def create


@cart = current_cart
product = Product.find(params[:product_id])
@line_item = @cart.line_items.build(:product_id => product.id )
respond_to do |format|
if @line_item.save
format.html { redirect_to @line_item.cart,
              notice: 'Line item was successfully created'}
format.json { render json: @line_item,
              status: :created, location: @line_item }
else
format.html { render action: "new" }
            format.json { render json: @line_item.errors,
               status: :unprocessable_entity }
   end
  end
end

最有可能的是,这一行
session[:cart\u id]=cart.cart\u id
应该是
session[:cart\u id]=cart.id
。不是编辑代码中的
cart\u id
而是
cart.id
是的,然后我又有问题了,请帮助我,“未初始化常量CartsController”这是另一个问题。看起来您没有为购物车定义控制器。我建议您放下这本书,慢慢地理解各种RubyonRails概念的基础知识。这本书可以在网上免费获得,它是最新的最佳实践,涵盖了新手在使用Rails时需要知道的一切