【发布时间】:2019-11-06 21:06:35
【问题描述】:
我想知道如何优雅地处理错误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
如果我发送的请求传递的不是包含至少一个 prop 的度量键的 json,rails 将自动使用 HTML 回复请求。我想覆盖它。
【问题讨论】:
标签: ruby-on-rails