编辑:这是POST数据和ruby日志:
Ruby日志:
Started PUT "/things/1" for 127.0.0.1 at 2014-10-28 10:54:32 -0700
Processing by ThingController#update as JSON
Parameters: {
"name"=>"Name",
"image_data"=>"data:image/png;base64,iVBORw0KG...",
"id"=>"1",
"Thing" => {
"name"=>"Name"
}
}
发布的JSON数据:
{
"name":"Name",
"image_data":"data:image/png;base64,iVBORalksdfj"
}
为什么ruby日志显示完整的数据,然后为什么Thing缺少我发送的额外字段?
image_data
:
class ThingController < ApplicationController
respond_to :html, :json
def update
@thing.update(thing_params)
respond_with(@thing)
end
def thing_params
params.require(:thing).permit(:name, :image_data) # allow it here
end
end
该image_data
字段实际上不在模型上,但是我为其创建了一个accesor:
class Thing < ActiveRecord::Base
attr_accessor :image_data
before_validation :decode_base64_image
def decode_base64_image
# Always empty!
logger.debug image_data
if image_data
# Never gets here
end
end
end
image_data
永远不会在注释行上设置。是什么赋予了?如何在验证器中访问一个字段(image_data)并将其按摩到另一个字段(实际上在模型上?)。
更改您的POST json结构,例如
{
"Thing" : {
"name" : "Name",
"image_data" : "data:image/png;base64,iVBORw0KG..."
}
}
所以image_data
将是Thing
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句