在我的购物车控制器中,我有:
class CartsController < ApplicationController
before_action :set_cart, only: [:show, :edit, :update, :destroy]
rescue_from ActiveRecord::RecordNotFound, with: :invalid_cart
并在controllers / concerns / current_cart.rb中我有:
module CurrentCart
extend ActiveSupport::Concern
private
def set_cart
@cart = Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
@cart = Cart.create
session[:cart_id] = @cart.id
end
但是当我点击“添加到购物车”时,我收到错误消息
"undefined method `set_cart' for #<CartsController"
有什么见解?感谢。
答案 0 :(得分:0)
您需要包含模块:
class CartsController < ApplicationController
include CurrentCart
before_action :set_cart, only: [:show, :edit, :update, :destroy]
rescue_from ActiveRecord::RecordNotFound, with: :invalid_cart