conditional convert options paperclip

后端 未结 5 1535
[愿得一人]
[愿得一人] 2021-02-06 09:18

After some research I was able to add styles based on my image_class column.

Model.rb

has_attached_file :image,
                    


        
5条回答
  •  没有蜡笔的小新
    2021-02-06 10:25

    The approved answer doesn't work. It can be read in the comments and the author acknowledges that it hasn't been tested in code.

    This code does work:

    has_attached_file :image,
                      :styles => lambda { |attachment|
    
                        thumb_convert_options = case attachment.instance.image_class
                          when "poster"
                            "-flop"
                          when "cover"
                            "-enhance"
                        end
    
                        {
                          thumb: {
                            convert_options: thumb_convert_options
                          }
                        }
                      }
    

    The correct approach is to have convert_options inside the styles lambda; having it as a separate lambda does not work, at least for Paperclip version 4.1 and higher.

    To keep my answer in one place, I've put all the code inline and omitted all styles beside thumb. Obviously to implement this you should keep the method decide_convert_options.

提交回复
热议问题