提问人:Ozge Cokyasar 提问时间:1/9/2019 最后编辑:Ozge Cokyasar 更新时间:1/9/2019 访问量:90
CarrierWave 图像上传器 - POST 请求发送正确的参数,但图像将图像保存为“image”:{“url”:null}
CarrierWave image uploader - POST request sends the correct parameters but image saves image as "image":{"url":null}
问:
问题: 我正在尝试对 Visions 提出 POST 请求。它有两个参数,这两个参数都在传递中。但是,它以这种格式保存(并且图像 url 始终为“null”):
{"data":{"user":{"id":26,"image":{"url":null},"description":"dsfsfdsfdsfsd"}}}
我需要做些什么才能将我传递的 image 参数保存为 image 对象中的 url 字符串?这是我的愿景表:
class CreateVisions < ActiveRecord::Migration[5.1]
def change
create_table :visions do |t|
t.string :image
t.text :description
t.timestamps
end
end
end
VisionsController.rb:
def create
@vision = current_user&.visions.build(vision_params)
@vision.save
render :create, status: :created
end
def vision_params
params.require(:vision).permit(:image, :description)
end
vision.json.jbuilder:
json.call(
vision,
:id,
:image,
:description
)
用户模态:
class User < ApplicationRecord
acts_as_token_authenticatable
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :trackable, :rememberable, :validatable
has_many :posts
has_many :visions
end
**视觉型号:**class Vision < ApplicationRecord
belongs_to :user
mount_uploader :image, ImageUploader
end
我是这样发出我的 POST 请求的:
let response = await fetch('https://prana-app.herokuapp.com/v1/visions/', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-User-Email': this.state.email,
'X-User-Token': this.state.accessToken
},
body: JSON.stringify({
vision: {
description: 'dsfsfdsfdsfsd',
image: this.state.uploadFile
}
})
});
当我发出请求时,它会接收以下参数:
Parameters: {"vision"=>{"description"=>"dsfsfdsfdsfsd", "image"=>"file:///BLABLABLA.jpg"}}
答: 暂无答案
评论
visin_params