【问题标题】:Paperclip Post request not uploading imagePaperclip Post 请求未上传图片
【发布时间】:2016-04-01 18:14:54
【问题描述】:

我正在尝试通过回形针中的发布请求上传图片。

我执行请求成功,但请求没有上传图片。

我的图片模型

class Picture < ActiveRecord::Base
    attr_accessor :images, :images_file_name
   has_attached_file :images  
   do_not_validate_attachment_file_type :images                                  
end

我的控制器

class PicturesController < ApplicationController     
  def event_desc_image
    @image_url = Picture.create!(images: params[:images])             
    render :json => @image_url      
  end      
end

在 Rails 控制台中,它会触发以下命令

[paperclip] saving /pictures/images/000/000/004/original/tcs.png
[AWS S3 200 3.948386 0 retries] put_object(:acl=>:public_read,:bucket_name=>"clot1",:content_length=>88200,:content_type=>"image/png",:data=>Paperclip::UploadedFileAdapter: tcs.png,:key=>"pictures/images/000/000/004/original/tcs.png")  

[paperclip] saving /pictures/images/000/000/004/medium/tcs.png
[AWS S3 200 0.878744 0 retries] put_object(:acl=>:public_read,:bucket_name=>"clot1",:content_length=>24067,:content_type=>"image/png",:data=>Paperclip::FileAdapter: f3dfe432021081dc257184b8a895464420160401-4627-1usx855,:key=>"pictures/images/000/000/004/medium/tcs.png")  

[paperclip] saving /pictures/images/000/000/004/thumb/tcs.png
[AWS S3 200 0.560126 0 retries] put_object(:acl=>:public_read,:bucket_name=>"clot1",:content_length=>4416,:content_type=>"image/png",:data=>Paperclip::FileAdapter: f3dfe432021081dc257184b8a895464420160401-4627-ui906j,:key=>"pictures/images/000/000/004/thumb/tcs.png") 

但它不保存任何图像数据。 对象看起来像:

+----+-----------------+--------------------+-----------------+------------------+-------------------------+-------------------------+---------+
| id | image_file_name | image_content_type | image_file_size | image_updated_at | created_at              | updated_at              | caption |
+----+-----------------+--------------------+-----------------+------------------+-------------------------+-------------------------+---------+
| 1  |                 |                    |                 |                  | 2016-04-01 17:55:05 UTC | 2016-04-01 17:55:05 UTC |         |
-------+-------------------------+-------------------------+---------+

请提出解决方案。 谢谢

【问题讨论】:

  • 您是否允许控制器中的参数中的 :images ?
  • 我正在使用 attr_accessor

标签: ruby-on-rails paperclip


【解决方案1】:

将 :image 添加到图片控制器的允许参数列表中。

【讨论】:

  • 我不能这样做,因为我得到以下格式参数的参数:参数:{“images”=>#
【解决方案2】:

以下解决方案有效

class PicturesController < ApplicationController 

  def event_desc_image
    @image = Picture.create!(photo_params)             
    render :json => @image      
  end

  private

  def photo_params
    params.permit(:image)
  end

end

谢谢@布莱恩

【讨论】:

  • 我从我的 ios 应用上传图片时遇到了同样的问题,请您提供您的多部分请求。我的情况是:(模型) has_attached_file :avatar_default 和 On ios app NSObject with attributs avatar_default_file_name |头像_default_content | avatar_default_content_type
【解决方案3】:

你看到了什么:

 return render json: params

作为 event_desc_image 第一行?

【讨论】:

    猜你喜欢
    • 2013-01-18
    • 1970-01-01
    • 2019-11-24
    • 1970-01-01
    • 2020-02-11
    • 2017-06-28
    • 2014-09-15
    • 2013-12-29
    • 1970-01-01
    相关资源
    最近更新 更多