CarrierWave 图像上传器 - POST 请求发送正确的参数,但图像将图像保存为“image”:{“url”:null}

CarrierWave image uploader - POST request sends the correct parameters but image saves image as "image":{"url":null}

提问人:Ozge Cokyasar 提问时间:1/9/2019 最后编辑:Ozge Cokyasar 更新时间:1/9/2019 访问量:90

问:

问题: 我正在尝试对 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"}}
Ruby-on-rails reactjs react-native ruby-on-rails-4 载波

评论

0赞 Vishal 1/9/2019
请添加有问题的方法和用户模型visin_params
0赞 Ozge Cokyasar 1/9/2019
@Vishal添加了vision_params和用户模式信息:)
0赞 Vishal 1/9/2019
如果要存储图像,为什么要将数据类型作为字符串?
0赞 Ozge Cokyasar 1/9/2019
@vishal,因为这是所有教程告诉我要做的
0赞 Vishal 1/9/2019
我认为你应该使用回形针来上传文件,如果你主动存储 ruby on rails 本身,那就更好了。

答: 暂无答案