我正在尝试更新 rails 中列 IsValid 和 Comments 的记录,但没有在后端更新或出现任何错误:
以下是我的参数结果:
Started PATCH "/metrics/1" for
Processing by MetricsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"1oC/1UdAaTPUepy1zIjO1x6n67Th/pdcnvYJH95cB63tZts9d135JIK4MzQD2/pdPoRKnUKXIc0ZI9MQZkjfNQ==",
"metrics_controller"=>{"IsValid"=>"False", "Comments"=>"1"}, "commit"=>"Save", "id"=>"1"}
Metric Load (75.1ms) SELECT `Threshold`.* FROM `Threshold` WHERE `Threshold`.`ID` = 1 LIMIT 1
Unpermitted parameters: utf8, _method, authenticity_token, metrics_controller, commit, id
(75.7ms) BEGIN
(75.1ms) COMMIT
控制器中的更新方法:
def update
@metric = Metric.find(params[:id])
#if(@metric.update(post_params))
if(@metric.update_attributes(post_params))
redirect_to metrics_path
else
render 'edit'
end
end
private def post_params
params.permit(:Metric, :WI, :Value, :UT, :Score, :IsValid, :UserName, :Comments)
end
如果我使用以下代码更新 post_params 方法,则会引发错误:
private def post_params
params.require(:metric).permit(:Metric, :WI, :Value, :UT, :Score, :IsValid, :UserName, :Comments)
end
来自请求(来自日志)的参数是:
{
"utf8"=>"✓",
"authenticity_token"=>"1oC/1UdAaTPUepy1zIjO1x6n67Th..",
"metrics_controller"=> {
"IsValid"=>"False",
"Comments"=>"1"
},
"commit"=>"Save",
"id"=>"1"
}
所以钥匙isValid
在钥匙下metrics_controller
。然后,有了这种即将到来的数据,在您的控制器中,您应该调用
params
.require(:metrics_controller)
.permit(:IsValid, :Comments, :id)
您应该注意命名约定(snake_case 而不是 CamelCase),但最重要的是负责此请求的表单应该根据您的控制器的期望命名输入。
然后,您应该使用类似的名称命名表单元素(根据您的屏幕截图)
<select name="metric[is_valid]">
<option value="1">Yes</option>
<option value="0">No</option>
</select>
使用这种命名方式,数据将被限定在 keymetric
而不是metric_controller
,并且它会通过强大的参数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句