ruby-on-rails 如何利用法拉第效应保存图像

mtb9vblg  于 2023-06-07  发布在  Ruby
关注(0)|答案(2)|浏览(293)

我有使用活动存储的作者类。

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"} }

作者正在保存无图像
出什么事了吗
谢谢

zlhcx6iw

zlhcx6iw1#

我在这里看到两个错误:
1.您必须像这样打开传递给Faraday::Multipart::FilePart的文件:

Faraday::Multipart::FilePart.new(File.open(Rails.root.join('app', 'assets',                  
                                           'images', 'about.jpg')), 
                                'image/jpeg')

1.您必须将图像移动到author对象中

`payload[:author][:image] = Faraday::Multipart::FilePart.new(...)`
bis0qfac

bis0qfac2#

我只是从头文件中删除了“Content-Type' => 'application/json”。这是@Alex在评论中提出的问题。

headers: {'Authorization' => "Bearer qAJiTqIJRou0"}

还有工作

相关问题