Rails 和 Carrierwave - 可以上传图像,但不能上传 pdf

Phi*_*899 1 ruby ruby-on-rails imagemagick carrierwave minimagick

我已经成功地使用载波上传图像文件。我希望表单能够接受图像文件和 pdf 文件。当我尝试上传 pdf 时,它不上传文件。它与该行有关:

process :resize_to_fill => [166,166]
Run Code Online (Sandbox Code Playgroud)

如果我把它去掉,pdf就可以了。问题是我需要那条线,因为我需要调整所有上传的图片的大小。这是上传者:

class PortfoliofileUploader < CarrierWave::Uploader::Base
      include CarrierWave::MiniMagick
      def store_dir
          "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
      end
      version :picture do
           process :resize_to_fill => [166,166]
      end
      def extension_white_list
          %w(jpg jpeg gif png pdf doc docx)
      end
end
Run Code Online (Sandbox Code Playgroud)

有谁知道我该如何修复它以便图像和 pdf 可以工作?谢谢。

更新:

作品集展示页面(2个版本):

版本1:

<% @portfolio.portfolio_pics.collect{|picture| picture.port_pic.picture}.each do |pic| %>                           
    <li><a href="#"><%= image_tag pic %></a></li>                       
<% end %>
Run Code Online (Sandbox Code Playgroud)

版本2:

<% @portfolio.portfolio_pics.each do |pic| %>
    <li><a href="#"><%= image_tag pic.port_pic.picture %></a></li>
<% end %>
Run Code Online (Sandbox Code Playgroud)

Ale*_*lke 5

Carrierwave 有一个解决方案,在自述文件中指出:

有条件的版本

有时,您希望限制模型内某些属性或基于图片本身的版本创建。

class MyUploader < CarrierWave::Uploader::Base

  version :human, :if => :is_human?
  version :monkey, :if => :is_monkey?
  version :banner, :if => :is_landscape?

protected

  def is_human? picture
    model.can_program?(:ruby)
  end

  def is_monkey? picture
    model.favorite_food == 'banana'
  end

  def is_landscape? picture
    image = MiniMagick::Image.open(picture.path)
    image[:width] > image[:height]
  end

end
Run Code Online (Sandbox Code Playgroud)

例子

例如,为了仅为图像创建拇指,我采用了此选项:

version :thumb, :if => :image? do
    process :resize_to_fit => [200, 200]
  end

protected    

    def image?(new_file)
      new_file.content_type.start_with? 'image'
    end
Run Code Online (Sandbox Code Playgroud)

在这种情况下,请确保包含 MimeTypes:

include CarrierWave::MimeTypes
Run Code Online (Sandbox Code Playgroud)