Weird paperclip error message

前端 未结 2 1922
無奈伤痛
無奈伤痛 2021-01-14 14:18

I have a Rails 3 app using Paperclip 2.3.8. I have the following specified in my model:

validates_attachment_content_type :file,
  :content_type => [\'ima         


        
相关标签:
2条回答
  • 2021-01-14 14:32

    I think I figured out the problem.

    Try removing the :styles from your model and you will see that the 'identify' error message goes way and the model validates as expected.

    The problem is Paperclip is processing the styles even though the content_type validation has failed. It tries to process your pdf as an image and then you get the error:

    /var/folders/cs/cs-jiL3ZH1WOkgLrcqa5Ck+++TI/-Tmp-/stream20110404-43533-vm7eza.pdf
    is not recognized by the 'identify' command.
    

    The solution is to skip the processing if the validation fails, by adding this to your model:

    before_post_process :skip_if_invalid
    
    def skip_if_invalid
      return false unless self.valid?
    end
    

    This way Paperclip won't try to turn files that are not images into thumbnails :)

    0 讨论(0)
  • 2021-01-14 14:46

    it's not a weird. It is the most popular error for paperclip. And it's not about paperclip, actually, but about ImageMagick.

    1. Did you install ImageMagick?
    2. Did you added image_magick command_path via initializer?

    If you have istalled IM poperly now checkout its location:

    which identify
    #=> it will return some path
    

    Create new file in your Rails application config/initializers/paperclip.rb:

    Paperclip.options[:command_path] = "path/to/identify"
    

    Also you can add :whiny => false option to your has_attached_file

    has_attached_file :picture, :styles => { ... }, :whiny => false
    

    So it won't throw any errors if something went wrong

    Also you can read here if you want to store pictures and files in one model and want to ignore styling for non-image files:

    Paperclip process images only

    0 讨论(0)
提交回复
热议问题