我有一个create方法,它通过关联建立新模型,并且我期望它在POST请求中没有参数的情况下返回400响应,并带有一些文本。但是,我得到一个错误。
这是在Rails 4.0.2中
控制器方法:
def create
@cast_profile = current_user.build_cast_profile(cast_profile_params)
if @cast_profile.save
redirect_to cast_profile_path
else
render :edit
end
end
def cast_profile_params
params.require(:cast_profile).permit(:name, :email, :public)
end
如果我通过了参数,那么一切正常,但是我正在尝试测试错误的请求方案。这是错误:
ActionController::ParameterMissing: param not found: cast_profile
我可以明确地对其进行挽救,但我认为应该使用强参数来自动做到这一点。
行为如下:
处理不允许的密钥
默认情况下,未明确允许的参数键将记录在开发和测试环境中。在其他环境中,这些参数将被简单地过滤掉并忽略。
此外,可以通过更改环境文件中的config.action_controller.action_on_unpermitted_parameters属性来更改此行为。如果设置为:log,将记录不允许的属性,如果设置为:raise,则会引发异常。
(来源)
我建议从具有400状态(错误请求)的异常中拯救出来:
rescue_from ActionController::ParameterMissing do
render :nothing => true, :status => :bad_request
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句