尝试从line_items控制器访问时,current_cart未定义?

时间:2014-09-21 01:29:54

标签: ruby-on-rails ruby ruby-on-rails-4

我正在尝试让用户在点击"添加到购物车"之后重定向到他们的current_cart。链接。我在行项目控制器中这样做,并使用railscasts源代码作为参考。 https://github.com/ryanb/railscasts-episodes/blob/master/episode-141/store/app/controllers/line_items_controller.rb

当我点击"添加到购物车"链接我收到此错误:

未定义的局部变量或方法`current_cart'

这是我的LineItemsController:

class LineItemsController < ApplicationController
  def create
  @product = Product.find(params[:product_id])
  @line_item = LineItem.create!(:cart => current_cart, :product => @product, 

  :quantity   => 1, :unit_price => @product.price)
  flash[:notice] = "Added #{@product.title} to cart."
  redirect_to current_cart_url
  end
end

这是我的CartsController:

class CartsController < ApplicationController
  def show
  @cart = current_cart
  end
end

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您的订单项控制器看起来缺少current_cart方法:

def current_cart
  # where you should find your current cart, i.e.
  @current_cart ||= Cart.find(session[:cart_id])
end