如何优雅地处理ActionController :: ParameterMissing

我想知道如何优雅地处理错误actionController::ParameterMissing,因此它不会返回丑陋的HTML,而是返回一条消息和与500 Internal Server Error不同的内容。

怎么知道我有这个控制器:

class MetricsController < ApplicationController
  before_action :set_metric,only: [:update]

  def update
    if @metric.update(metric_params)
      render json: @metric
    else
      render json: @metric.errors,status: :unprocessable_entity
    end
  end

  private

  def set_metric
    @metric = Metric.find_by(site: params[:site_id],post_id: params[:post_id])
  end

  def metric_params
    params.require(:metric).permit(:param1,:param2)
  end
end

并且如果我发送的请求通过的不是json,而json包含带有至少一个prop的度量键,Rails会自动以HTML答复请求。我想覆盖它。

xutuzi 回答:如何优雅地处理ActionController :: ParameterMissing

这可能会帮助Rails 5 way to handle ActionController::ParameterMissing

import tensorflow as tf
import os
from tensorflow.python.framework.versions import CXX11_ABI_FLAG

CXX11_ABI_FLAG

bdfs_file_system_library = os.path.join("/opt/bluedata","libbdfs_file_system_shared_r1_9.so")
tf.load_file_system_library(bdfs_file_system_library)

with tf.gfile.Open("dtap://TenantStorage/tmp/tensorflow/dtap.txt",'w') as f:
    f.write("This is the dtap test file")

with tf.gfile.Open("dtap://TenantStorage/tmp/tensorflow/dtap.txt",'r') as f:
    content = f.read() 
,
  

Rails提供了一种清除控制器中异常的干净方法,将特定的错误类映射到相应的处理程序。

来自https://simonecarletti.com/blog/2009/12/inside-ruby-on-rails-rescuable-and-rescue_from/

因此,实际上您可以使用Travis提到的rescuerescue_from,例如

rescue_from ActionController::ParameterMissing,:with => :render_404

本文链接:https://www.f2er.com/3148956.html

大家都在问