【问题标题】:How to display an error message on a destroy action, since it redirects to index action?如何在销毁操作上显示错误消息,因为它重定向到索引操作?
【发布时间】:2012-03-14 07:04:58
【问题描述】:

在我的模型中:

before_destroy :ensure_not_referenced_by_any_shopping_cart_item

def ensure_not_referenced_by_any_shopping_cart_item
  unless shopping_cart_items.empty?
    errors.add(:base, "This item can't be deleted because it is present in a shopping cart")
    false
  end
end

当商品出现在购物车中时,它不会被销毁(这很好),如果我在操作中记录它会看到错误..

def destroy
  @product = Beverage.find(params[:id])
  @product.destroy

  logger.debug "--- error: #{@product.errors.inspect}"

  respond_to do |format|
    format.html { redirect_to beverages_url }
    format.json { head :ok }
  end
end

..但是当redirect_to发生时,设置错误消息的实例变量被放弃,所以用户永远看不到它。

应该如何将错误消息保留到下一个操作,以便它可以显示在其视图中?

谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 error-handling destroy


    【解决方案1】:

    我建议使用 Flash 消息来传递错误信息。

    respond_to do |format|
      format.html { redirect_to beverages_url, :alert => "An Error Occurred! #{@products.errors[:base].to_s}"
      format.json { head :ok }
    end
    

    类似的东西。这就是我在自己的应用程序中处理类似问题的方式,但这取决于您要向用户显示的信息的详细信息。

    【讨论】:

    • 感谢您在回答中包含#{@products.errors[:base].to_s} :)
    【解决方案2】:

    您需要将错误放入闪存中。大概是这样的

    def destroy
      @product = Beverage.find(params[:id])
      if @product.destroy
        message = "Product destroyed successfully"
      else
        message = "Product could not be destroyed"
      end
    
    
      respond_to do |format|
        format.html { redirect_to beverages_url, :notice => message }
        format.json { head :ok }
      end
    end
    

    请注意,您还需要在 application.html.erb 文件中打印消息。

    【讨论】:

    • 谢谢,@cailinanne,您和@Justin Herrick 的回答都是正确的,但我接受了他的回答,因为他来得早。不过,我仍然将您的标记为“有用”.. :) 另外,感谢您提供有关 application.html.erb 的提示!
    【解决方案3】:

    您可以使用两条消息来执行此操作,一条状态为 OK,另一条为 NO OK(unprocessable_entity 例如,here's more)。

    def destroy
      @product = Beverage.find(params[:id])
    
    
      respond_to do |format|
        if @product.destroy
         format.html { redirect_to beverages_url, notice: "Product destroyed successfully", status: :ok}
         format.json { head :ok, status: :ok}
        else
          format.html { redirect_to beverages_url, alert: "Product could not be destroyed", status: :unprocessable_entity}
          format.json {head :no_content, status: :unprocessable_entity }
        end
      end
    end
    

    【讨论】:

      【解决方案4】:

      在 Rails 4 中,你可以这样做

      def destroy
        @product = Beverage.find(params[:id])
      
        respond_to do |format|
          if @product.destroy
            format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
            format.json { head :ok }
          else
            format.html { render :show }
            format.json { render json: @product.errors, status: :unprocessable_entity }
          end
        end
      end
      

      【讨论】:

        猜你喜欢
        • 2023-03-17
        • 1970-01-01
        • 2013-04-04
        • 1970-01-01
        • 2014-04-16
        • 1970-01-01
        • 2022-07-10
        • 2015-11-25
        • 1970-01-01
        相关资源
        最近更新 更多