回形针不上传图片,回滚

时间:2017-07-07 17:24:07

标签: ruby-on-rails ruby windows

Paperclip不上传图像。打印附加图像的对象的输出,表明没有上传图像。

控制台:

irb(main):011:0> b = Article.find(9)
  Article Load (0.5ms)  SELECT  "articles".* FROM "articles" WHERE "articles"."id" = ? LIMIT ?  [["id", 9], ["LIMIT", 1]]
=> #<Article id: 9, title: "Rails", body: "Server", created_at: "2017-07-07 15:05:41", updated_at: "2017-07-07 15:05:41", image_
file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil>
irb(main):012:0>

日志:

Command :: file -b --mime "C:/Users/addis/AppData/Local/Temp/4d3d12fcec1dd8d0eff643aa438c2b9120170708-9872-8pkize.jpg"
[paperclip] Content Type Spoof: Filename 2006-05-09_01.19.32.jpg (image/jpeg from Headers, ["image/jpeg"] from Extension), conte
nt type discovered from file command: . See documentation to allow this combination.
Command :: file -b --mime "C:/Users/addis/AppData/Local/Temp/4d3d12fcec1dd8d0eff643aa438c2b9120170708-9872-knt072.jpg"
[paperclip] Content Type Spoof: Filename 2006-05-09_01.19.32.jpg (image/jpeg from Headers, ["image/jpeg"] from Extension), conte
nt type discovered from file command: . See documentation to allow this combination.
   (0.0ms)  rollback transaction
Redirected to http://localhost:3000/articles/1
Completed 302 Found in 49ms (ActiveRecord: 0.5ms)

辅助方法:

module ArticlesHelper

  def article_params
    params.require(:article).permit(:title, :body, :tag_list, :image)
  end

end

型号:

class Article < ApplicationRecord
    has_many :comments
    has_many :taggings
    has_many :tags, through: :taggings
    has_attached_file :image
    validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png"]

    def tag_list
    self.tags.collect do |tag|
        tag.name
    end.join(", ")
    end
    def tag_list=(tags_string)
        tag_names = tags_string.split(",").collect{|s| s.strip.downcase}.uniq
        new_or_found_tags = tag_names.collect {|name| Tag.find_or_create_by(name: name)}
        self.tags = new_or_found_tags
    end 
end

使用Visual Code在Windows 10上运行。

遵循guide

1 个答案:

答案 0 :(得分:0)

我认为如果它是回滚,问题来自您的验证。 下面是validates_attachment的示例代码格式,您可以尝试

has_attached_file :image
validates_attachment :image, :content_type => { :content_type => ["application/octet-stream","image/jpg","image/jpeg","image/png"] }