我想知道如何优雅地处理错误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答复请求。我想覆盖它。