Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 轨道。未定义的方法`删除产品';零级:零级_Ruby On Rails - Fatal编程技术网

Ruby on rails 轨道。未定义的方法`删除产品';零级:零级

Ruby on rails 轨道。未定义的方法`删除产品';零级:零级,ruby-on-rails,Ruby On Rails,应用程序型号: 购物车有很多:行项目 产品有很多:行项目 行项目属于:购物车,属于:产品 如果同一产品多次添加到购物车中,我会在cart.rb中计算该数字: def add_product(product) current_item = line_items.find_by(product_id: product.id) if current_item current_item.quantity += 1 else current_item =

应用程序型号: 购物车
有很多:行项目
产品
有很多:行项目
行项目
属于:购物车
属于:产品

如果同一产品多次添加到购物车中,我会在cart.rb中计算该数字:

  def add_product(product)
    current_item = line_items.find_by(product_id: product.id)
    if current_item
      current_item.quantity += 1
    else
      current_item = line_items.build(product_id: product.id)
    end
    current_item
  end
  def create
    product = Product.find(params[:product_id])
    @line_item = @cart.add_product(product)

    respond_to do |format|
      if @line_item.save
        format.html { redirect_to store_index_url }
        format.js   { @highlited_item = @line_item }
        format.json { render :show, status: :created, location: @line_item }
      else
        format.html { render :new }
        format.json { render json: @line_item.errors, status: :unprocessable_entity }
      end
    end
  end
<td><%= link_to "+", line_items_path(product_id: line_item.product), method: :post, remote: true %></td>
  def decrease
    product = Product.find(params[:product_id])
    @line_item = @cart.remove_product(product)
  end
class LineItemsController < ApplicationController
      include CurrentCart
      before_action :set_cart, only: [:create]
      before_action :set_line_item, only: [:show, :edit, :update, :destroy]

      def index
        @line_items = LineItem.all
      end

      def show
      end

      def new
        @line_item = LineItem.new
      end

      def edit
      end

      def create
        product = Product.find(params[:product_id])
        @line_item = @cart.add_product(product)

        respond_to do |format|
          if @line_item.save
            format.html { redirect_to store_index_url }
            format.js   { @highlited_item = @line_item }
            format.json { render :show, status: :created, location: @line_item }
          else
            format.html { render :new }
            format.json { render json: @line_item.errors, status: :unprocessable_entity }
          end
        end
      end

      def update
        respond_to do |format|
          if @line_item.update(line_item_params)
            format.html { redirect_to @line_item, notice: 'Line item was successfully updated.' }
            format.json { render :show, status: :ok, location: @line_item }
          else
            format.html { render :edit }
            format.json { render json: @line_item.errors, status: :unprocessable_entity }
          end
        end
      end

      def destroy
        @line_item.destroy
        respond_to do |format|
          format.html { redirect_to line_items_url, notice: 'Line item was successfully destroyed.' }
          format.json { head :no_content }
        end
      end


      private
        def set_line_item
          @line_item = LineItem.find(params[:id])
        end

        def line_item_params
          params.require(:line_item).permit(:product_id)
        end
    end
现在我想给用户增加/减少这个数字的能力。进展顺利:

行项目\u控制器.rb

  def add_product(product)
    current_item = line_items.find_by(product_id: product.id)
    if current_item
      current_item.quantity += 1
    else
      current_item = line_items.build(product_id: product.id)
    end
    current_item
  end
  def create
    product = Product.find(params[:product_id])
    @line_item = @cart.add_product(product)

    respond_to do |format|
      if @line_item.save
        format.html { redirect_to store_index_url }
        format.js   { @highlited_item = @line_item }
        format.json { render :show, status: :created, location: @line_item }
      else
        format.html { render :new }
        format.json { render json: @line_item.errors, status: :unprocessable_entity }
      end
    end
  end
<td><%= link_to "+", line_items_path(product_id: line_item.product), method: :post, remote: true %></td>
  def decrease
    product = Product.find(params[:product_id])
    @line_item = @cart.remove_product(product)
  end
class LineItemsController < ApplicationController
      include CurrentCart
      before_action :set_cart, only: [:create]
      before_action :set_line_item, only: [:show, :edit, :update, :destroy]

      def index
        @line_items = LineItem.all
      end

      def show
      end

      def new
        @line_item = LineItem.new
      end

      def edit
      end

      def create
        product = Product.find(params[:product_id])
        @line_item = @cart.add_product(product)

        respond_to do |format|
          if @line_item.save
            format.html { redirect_to store_index_url }
            format.js   { @highlited_item = @line_item }
            format.json { render :show, status: :created, location: @line_item }
          else
            format.html { render :new }
            format.json { render json: @line_item.errors, status: :unprocessable_entity }
          end
        end
      end

      def update
        respond_to do |format|
          if @line_item.update(line_item_params)
            format.html { redirect_to @line_item, notice: 'Line item was successfully updated.' }
            format.json { render :show, status: :ok, location: @line_item }
          else
            format.html { render :edit }
            format.json { render json: @line_item.errors, status: :unprocessable_entity }
          end
        end
      end

      def destroy
        @line_item.destroy
        respond_to do |format|
          format.html { redirect_to line_items_url, notice: 'Line item was successfully destroyed.' }
          format.json { head :no_content }
        end
      end


      private
        def set_line_item
          @line_item = LineItem.find(params[:id])
        end

        def line_item_params
          params.require(:line_item).permit(:product_id)
        end
    end
行\u item.html.erb

  def add_product(product)
    current_item = line_items.find_by(product_id: product.id)
    if current_item
      current_item.quantity += 1
    else
      current_item = line_items.build(product_id: product.id)
    end
    current_item
  end
  def create
    product = Product.find(params[:product_id])
    @line_item = @cart.add_product(product)

    respond_to do |format|
      if @line_item.save
        format.html { redirect_to store_index_url }
        format.js   { @highlited_item = @line_item }
        format.json { render :show, status: :created, location: @line_item }
      else
        format.html { render :new }
        format.json { render json: @line_item.errors, status: :unprocessable_entity }
      end
    end
  end
<td><%= link_to "+", line_items_path(product_id: line_item.product), method: :post, remote: true %></td>
  def decrease
    product = Product.find(params[:product_id])
    @line_item = @cart.remove_product(product)
  end
class LineItemsController < ApplicationController
      include CurrentCart
      before_action :set_cart, only: [:create]
      before_action :set_line_item, only: [:show, :edit, :update, :destroy]

      def index
        @line_items = LineItem.all
      end

      def show
      end

      def new
        @line_item = LineItem.new
      end

      def edit
      end

      def create
        product = Product.find(params[:product_id])
        @line_item = @cart.add_product(product)

        respond_to do |format|
          if @line_item.save
            format.html { redirect_to store_index_url }
            format.js   { @highlited_item = @line_item }
            format.json { render :show, status: :created, location: @line_item }
          else
            format.html { render :new }
            format.json { render json: @line_item.errors, status: :unprocessable_entity }
          end
        end
      end

      def update
        respond_to do |format|
          if @line_item.update(line_item_params)
            format.html { redirect_to @line_item, notice: 'Line item was successfully updated.' }
            format.json { render :show, status: :ok, location: @line_item }
          else
            format.html { render :edit }
            format.json { render json: @line_item.errors, status: :unprocessable_entity }
          end
        end
      end

      def destroy
        @line_item.destroy
        respond_to do |format|
          format.html { redirect_to line_items_url, notice: 'Line item was successfully destroyed.' }
          format.json { head :no_content }
        end
      end


      private
        def set_line_item
          @line_item = LineItem.find(params[:id])
        end

        def line_item_params
          params.require(:line_item).permit(:product_id)
        end
    end
行项目\u控制器.rb

  def add_product(product)
    current_item = line_items.find_by(product_id: product.id)
    if current_item
      current_item.quantity += 1
    else
      current_item = line_items.build(product_id: product.id)
    end
    current_item
  end
  def create
    product = Product.find(params[:product_id])
    @line_item = @cart.add_product(product)

    respond_to do |format|
      if @line_item.save
        format.html { redirect_to store_index_url }
        format.js   { @highlited_item = @line_item }
        format.json { render :show, status: :created, location: @line_item }
      else
        format.html { render :new }
        format.json { render json: @line_item.errors, status: :unprocessable_entity }
      end
    end
  end
<td><%= link_to "+", line_items_path(product_id: line_item.product), method: :post, remote: true %></td>
  def decrease
    product = Product.find(params[:product_id])
    @line_item = @cart.remove_product(product)
  end
class LineItemsController < ApplicationController
      include CurrentCart
      before_action :set_cart, only: [:create]
      before_action :set_line_item, only: [:show, :edit, :update, :destroy]

      def index
        @line_items = LineItem.all
      end

      def show
      end

      def new
        @line_item = LineItem.new
      end

      def edit
      end

      def create
        product = Product.find(params[:product_id])
        @line_item = @cart.add_product(product)

        respond_to do |format|
          if @line_item.save
            format.html { redirect_to store_index_url }
            format.js   { @highlited_item = @line_item }
            format.json { render :show, status: :created, location: @line_item }
          else
            format.html { render :new }
            format.json { render json: @line_item.errors, status: :unprocessable_entity }
          end
        end
      end

      def update
        respond_to do |format|
          if @line_item.update(line_item_params)
            format.html { redirect_to @line_item, notice: 'Line item was successfully updated.' }
            format.json { render :show, status: :ok, location: @line_item }
          else
            format.html { render :edit }
            format.json { render json: @line_item.errors, status: :unprocessable_entity }
          end
        end
      end

      def destroy
        @line_item.destroy
        respond_to do |format|
          format.html { redirect_to line_items_url, notice: 'Line item was successfully destroyed.' }
          format.json { head :no_content }
        end
      end


      private
        def set_line_item
          @line_item = LineItem.find(params[:id])
        end

        def line_item_params
          params.require(:line_item).permit(:product_id)
        end
    end
完整行项目\u控制器.rb

  def add_product(product)
    current_item = line_items.find_by(product_id: product.id)
    if current_item
      current_item.quantity += 1
    else
      current_item = line_items.build(product_id: product.id)
    end
    current_item
  end
  def create
    product = Product.find(params[:product_id])
    @line_item = @cart.add_product(product)

    respond_to do |format|
      if @line_item.save
        format.html { redirect_to store_index_url }
        format.js   { @highlited_item = @line_item }
        format.json { render :show, status: :created, location: @line_item }
      else
        format.html { render :new }
        format.json { render json: @line_item.errors, status: :unprocessable_entity }
      end
    end
  end
<td><%= link_to "+", line_items_path(product_id: line_item.product), method: :post, remote: true %></td>
  def decrease
    product = Product.find(params[:product_id])
    @line_item = @cart.remove_product(product)
  end
class LineItemsController < ApplicationController
      include CurrentCart
      before_action :set_cart, only: [:create]
      before_action :set_line_item, only: [:show, :edit, :update, :destroy]

      def index
        @line_items = LineItem.all
      end

      def show
      end

      def new
        @line_item = LineItem.new
      end

      def edit
      end

      def create
        product = Product.find(params[:product_id])
        @line_item = @cart.add_product(product)

        respond_to do |format|
          if @line_item.save
            format.html { redirect_to store_index_url }
            format.js   { @highlited_item = @line_item }
            format.json { render :show, status: :created, location: @line_item }
          else
            format.html { render :new }
            format.json { render json: @line_item.errors, status: :unprocessable_entity }
          end
        end
      end

      def update
        respond_to do |format|
          if @line_item.update(line_item_params)
            format.html { redirect_to @line_item, notice: 'Line item was successfully updated.' }
            format.json { render :show, status: :ok, location: @line_item }
          else
            format.html { render :edit }
            format.json { render json: @line_item.errors, status: :unprocessable_entity }
          end
        end
      end

      def destroy
        @line_item.destroy
        respond_to do |format|
          format.html { redirect_to line_items_url, notice: 'Line item was successfully destroyed.' }
          format.json { head :no_content }
        end
      end


      private
        def set_line_item
          @line_item = LineItem.find(params[:id])
        end

        def line_item_params
          params.require(:line_item).permit(:product_id)
        end
    end
class LineItemsController
为什么我在
reduce
操作中得到未定义的方法
remove\u product

非常感谢

nil:NilClass的未定义方法“remove_product”

该错误是因为
@cart
未在
减少
方法中定义。由于您在执行操作之前已使用
设置了
@cart
,因此需要按如下方式调整它,以便
@cart
也可用于
减少
方法

before_action :set_cart, only: [:create, :decrease]
这会解决你的问题

但是为什么add_乘积方法有效呢


问题不在于
删除产品
添加产品
,而在于
@cart
未在
减少
方法中定义。由于
@cart
对于
create
方法是可用的,因此它没有触发错误

@cart
未定义。如果您在采取行动之前设置了
,那么您需要
减少
方法。@J.D.我更新了我的答案,为您在问题下的评论提供了解释。请检查一下。我是在你发布答案的时候发现的:谢谢!