我有使用活动存储的作者类。
class Author < ApplicationRecord
has_one_attached :image
end
这是我的法拉第连接
conn = Faraday.new(
url: 'http://localhost:3000/',
headers: {'Content-Type' =>
'application/json','Authorization' => "Bearer
qAJiTqIJRou0"} ) do |f|
f.request :multipart
f.adapter :net_http
end
允许参数
def author_params
params.require(:author).permit(:image, :title,
:full_name)
end
职位要求
require 'faraday'
require 'faraday/multipart'
payload = { author: {title: "lo"} }
payload[:author][:image] = Faraday::Multipart::FilePart.new(Rails.root.join('app', 'assets',
'images', 'about.jpg'), 'image/jpeg')
conn.post('/api/v1/authors', payload.to_json)
我收到的参数
Unpermitted parameter: :image. Context: { controller:
Api::V1::AuthorsController, action: create, request: #
<ActionDispatch::Request:0x00007f85583d7860>, params:
{"author"=>{"title"=>"foo iiiiiii", "full_name"=>"tony", "image"=>{"content_type"=>"image/jpeg", "original_filename"=>"about.jpg", "local_path"=>"/home/kashif/ticket_plot_api/app/assets/images/about.jpg", "io"=>"#<File:0x00007f78f416abd8>", "opts"=>{}}}, "controller"=>"api/v1/authors", "action"=>"create"} }
作者正在保存无图像
出什么事了吗
谢谢
2条答案
按热度按时间zlhcx6iw1#
我在这里看到两个错误:
1.您必须像这样打开传递给
Faraday::Multipart::FilePart
的文件:1.您必须将图像移动到
author
对象中bis0qfac2#
我只是从头文件中删除了“Content-Type' => 'application/json”。这是@Alex在评论中提出的问题。
还有工作