由于销毁操作重定向到索引操作,如何在销毁操作上显示错误消息?

发布于 2025-01-08 19:17:16 字数 788 浏览 0 评论 0原文

在我的模型中:

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 发生时,设置的消息将被放弃,因此用户永远不会看到它。

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

谢谢!

In a model I have:

before_destroy :ensure_not_referenced_by_any_shopping_cart_item

and

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

When the item is present in a cart, it is not destroyed (which is good), and I see the error if I log it in the action..

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

..but the instance variable on which the error message was set is abandoned when the redirect_to happens, so the user never sees it.

How should the error message be persisted to the next action so it can be shown in its view?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

瘫痪情歌 2025-01-15 19:17:16

我建议使用闪存消息来转发错误信息。

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

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

I would recommend using a flash message to relay the error information.

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

Something to that effect. That is how I have handled similar issues in my own apps, but it depends on the detail of information you want to display to the user.

她说她爱他 2025-01-15 19:17:16

您需要将错误放入闪存中。大致类似于

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 文件中打印消息。

You need to put the error in the flash. Something roughly like

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

Note that you also need to be printing out messages in your application.html.erb file.

追星践月 2025-01-15 19:17:16

您可以使用两条消息来完成此操作,一条消息的状态为 OK,另一条消息的状态为 NO OK(unprocessable_entity,例如 这里还有更多)。

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

You can do it with two messages, one with the status OK, and another with NO OK (unprocessable_entity for example, 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
失去的东西太少 2025-01-15 19:17:16

在 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

In Rails 4, you can do like that

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文