我试图让 Paperclip 在表单提交时从我的节日模型将图像上传到 s3,但我收到了Unpermitted 参数:image。错误
我检查了强大的参数,模型内容验证并阅读了回形针文档,但无济于事。
我想我已将问题缩小到我对数据库的发布请求无法处理分配给festival.image 的文件对象,但无法弄清楚我将如何在发布请求中表示这一点。
我正在使用前端的 Rails 上的 react 以 Rails 作为后端来捕获 Rails 中的数据。我跟着这个示例代码https://github.com/carlbaron/react-file-upload-demo
我还使用 React-dropzone 来捕获上传的文件,它为图像预览添加了预览属性。
已经坚持了一段时间了,非常感谢任何帮助!
打印到控制台的发布请求的开头
Processing by FestivalsController#create as JSON
Parameters: {"festival"=>{"fest_name"=>"Test Festival", "image"=>{"preview"=>"blob:http://localhost:5000/76b95cb5-45bf-46a9-ba7b-f5b9ad127521"}}}
| Unpermitted parameter: image
postFestival(festival) {
let config = {
responseType: 'json',
processData: false,
contentType: false,
headers: ReactOnRails.authenticityHeaders(),
};
let str = JSON.stringify(festival);
console.log("ENTITY IS " + str);
//returns
//ENTITY IS {"fest_name":"Test Festival","image":{"preview":"blob:http://localhost:5000/76b95cb5-45bf-46a9-ba7b-f5b9ad127521"}}
return(
request.post('/festivals/create', {festival}, config)
);
},
节日.rb
class Festival < ApplicationRecord
has_attached_file :image, default_url: "/assets/ASOT-COVER.png"
validates_attachment :image,
content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }
end
节日控制器
def create
@festival = Festival.create(festival_params)
puts "festival.image =" + @festival.image.inspect
#returns = festival.image =#<Paperclip::Attachment:0x007fc288868bf0 @name=:image, @name_string="image", @instance=#
if @festival.save
puts "Festival SAved = + " + @festival.inspect
#returns the festival object saved to the DB minus the image param
else
respond_to do |format|
format.json { render json: @festival.errors, status: :unprocessable_entity}
puts "ERROR = " + @festival.errors.inspect
end
end
private
def festival_params
params.require(:festival).permit(:fest_name, :fest_organizer, :fest_location,
:fest_date, :fest_url, :fest_venue, :fest_description,
:image)
end
end