gpt4 book ai didi

javascript - rails 5 : allow javascript posted image from controller

转载 作者:行者123 更新时间:2023-12-03 02:32:19 25 4
gpt4 key购买 nike

我正在通过 Javascript 将一张图像(或多张图像)发布到我的 Controller 。我使用 Carrierwave + Rails 5 进行图像上传。

图像在参数中出现如下:

<ActionController::Parameters {"file"=><ActionController::Parameters {"0"=>#<ActionDispatch::Http::UploadedFile:0x00007fcc92da6c70 @tempfile=#<Tempfile:/var/folders/3m/9jgntpps1j5gzkm444tq6t400000gn/T/RackMultipart20180208-2884-15wx288.jpeg>, @original_filename="1.jpeg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"file[0]\"; filename=\"1.jpeg\"\r\nContent-Type: image/jpeg\r\n">} permitted: false>, "controller"=>"listings", "action"=>"create"} permitted: false>

我试图以某种方式允许它进入我的 Controller 。这似乎失败了:

@listing.pictures = params[:file]
ActiveRecord::AssociationTypeMismatch: Picture(#70258317227420) expected, got "0" which is an instance of String(#70258262105620)

这似乎也失败了:

@listing.pictures = params[:file]['0']
NoMethodError: undefined method `each' for #<ActionDispatch::Http::UploadedFile:0x00007fcc92da6c70>

我不确定如何通过此 POST 请求将图片添加到我的模型中。有什么想法吗?

Listing 和 Picture 之间的关系如下:

class Listing < ApplicationRecord
has_many :pictures, as: :imageable, dependent: :destroy
end

class Picture < ApplicationRecord
belongs_to :imageable, polymorphic: true
mount_uploader :image, ImageUploader
end

提前致谢!

最佳答案

尝试以下操作:

@listing.pictures = params[:file].values.map {|file| Picture.new(image: file)}

关于javascript - rails 5 : allow javascript posted image from controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48684017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com