Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.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 3 如何在另一个类的控制器中销毁一个类的实例变量_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 3 如何在另一个类的控制器中销毁一个类的实例变量

Ruby on rails 3 如何在另一个类的控制器中销毁一个类的实例变量,ruby-on-rails-3,Ruby On Rails 3,我正在用Rails做敏捷Web开发的一章练习。这是一个商店,里面有你可以添加到购物车的产品。购物车中的产品称为LineItem。您可以删除购物车内的单个行项目。当最后一行项目被删除时,我希望整个购物车被销毁。我的代码不起作用: CartsController def destroy @cart = current_cart @cart.destroy session[:cart_id] = nil respond_to do |format| format.html {

我正在用Rails做敏捷Web开发的一章练习。这是一个商店,里面有你可以添加到购物车的产品。购物车中的产品称为LineItem。您可以删除购物车内的单个行项目。当最后一行项目被删除时,我希望整个购物车被销毁。我的代码不起作用:

CartsController

def destroy
  @cart = current_cart
  @cart.destroy
  session[:cart_id] = nil

  respond_to do |format|
    format.html { redirect_to store_path }
    format.json { head :no_content }
  end
end
def destroy
  @cart = current_cart
  @line_item = LineItem.find(params[:id])
  quantity = @line_item.quantity

  if quantity > 1
    quantity -= 1
    @line_item.update_attribute(:quantity, quantity)
  else
    @line_item.destroy
  end

  respond_to do |format|
    format.html {
      if @cart.line_items.empty?
        @cart.destroy
      else
        redirect_to @cart
      end
    }
    format.json { head :no_content }
  end
end
LineItemsController

def destroy
  @cart = current_cart
  @cart.destroy
  session[:cart_id] = nil

  respond_to do |format|
    format.html { redirect_to store_path }
    format.json { head :no_content }
  end
end
def destroy
  @cart = current_cart
  @line_item = LineItem.find(params[:id])
  quantity = @line_item.quantity

  if quantity > 1
    quantity -= 1
    @line_item.update_attribute(:quantity, quantity)
  else
    @line_item.destroy
  end

  respond_to do |format|
    format.html {
      if @cart.line_items.empty?
        @cart.destroy
      else
        redirect_to @cart
      end
    }
    format.json { head :no_content }
  end
end
这会产生以下错误:

Template is missing

Missing template line_items/destroy, application/destroy with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/home/mike/Projects/depot/app/views"
当购物车被毁时,我想在store_path结束

谢谢, mike

查看添加内容(作为注释):


好的,在普鲁斯旺有很多。这很有效。但我不明白为什么Cart.destroy中的重定向没有任何作用?因为当您从第_item#destroy行调用@Cart.destroy时,它仍然默认为第_item#destroy行中的呈现逻辑(如果未指定任何内容,则为destroy操作的查看页面),除非您专门重定向到destroy路径或其他路径