强对称要求

时间:2018-05-17 04:56:05

标签: javascript ruby-on-rails vue.js ecmascript-6 ruby-on-rails-5

考虑到这些强大的障碍:

def image_params
  params.require(:image).permit(:data, :x, :y, :width, :height)
end

:image到底应该是什么?我是这样从前端提交的:

updateImage: function (e) {
    e.preventDefault()
    var formData = new FormData()
    formData.append(`x`, this.crop_x)
    formData.append(`y`, this.crop_y)
    formData.append(`width`, this.crop_width)
    formData.append(`height`, this.crop_height)
    formData.append(`image`, this.imageID)
    this.$http.patch(`/articles/${this.id}/images/${this.imageID}`, formData)
}

此处假设:image应为16

之类的ID

1 个答案:

答案 0 :(得分:1)

使用strong_params时,您不一定要指定数据类型,您只需设置哪些属性是强制性的以及哪些属性是允许的规则,

在您的示例中,image是必需属性,如果参数中缺少该属性,您将收到错误,而permit :data, :x, :y, :width, :height则为白色 - 列出他们说他们可以安全使用或通过。

您可能希望以这种方式创建它,而不是执行追加,

{image: {data: '', x: '', y: '', width: '', height: ''}}

希望这有帮助

相关问题