问题: 我正在尝试向Visions发出POST请求。它有两个参数都正在传递。但是,它以这种格式保存(图片网址始终为“ null”):
{"data":{"user":{"id":26,"image":{"url":null},"description":"dsfsfdsfdsfsd"}}}
我需要怎么做才能将要传递的图像参数保存为图像对象中的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"}}