没有将String隐式转换为Array - Rails 4.1.0.beta1

时间:2014-06-19 16:01:03

标签: ruby-on-rails ruby-on-rails-4 paperclip type-conversion watermark

我一直在处理一个我收到错误的问题:"没有将String隐式转换为数组"。

把它放在上下文中:我在Rails 4.1.0.beta1上,我正在使用Paperclip。我正在尝试为我的图像添加水印。我找到了以下解决方案:Watermark images with paperclip, rails 4

我收到这里声明的错误信息后,昨晚有点工作了 - 然后我做了一些改动,我已经撤消了。现在我再次收到以下错误?!这可能很简单,但我似乎没有看到它。

非常感谢任何帮助。

以下是错误消息:

Completed 500 Internal Server Error in 578ms TypeError - no implicit conversion of String into Array: () Users/georg/Development/RoR/lp/lib/paperclip_processors/watermark.rb:44:in `make' paperclip (4.1.1) lib/paperclip/processor.rb:33:in `make' 
paperclip (4.1.1) lib/paperclip/attachment.rb:462:in `block in post_process_style' 
paperclip (4.1.1) lib/paperclip/attachment.rb:461:in `post_process_style' 
paperclip (4.1.1) lib/paperclip/attachment.rb:454:in `block in post_process_styles' 
paperclip (4.1.1) lib/paperclip/attachment.rb:453:in `post_process_styles' 
paperclip (4.1.1) lib/paperclip/attachment.rb:445:in `block (2 levels) in post_process'
activesupport (4.1.0.beta1) lib/active_support/callbacks.rb:113:in `call' 
activesupport (4.1.0.beta1) lib/active_support/callbacks.rb:166:in `block in halting' 
activesupport (4.1.0.beta1) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional' 
activesupport (4.1.0.beta1) lib/active_support/callbacks.rb:86:in `run_callbacks' 
paperclip (4.1.1) lib/paperclip/callbacks.rb:36:in `run_paperclip_callbacks' 
paperclip (4.1.1) lib/paperclip/attachment.rb:443:in `block in post_process' 
activesupport (4.1.0.beta1) lib/active_support/callbacks.rb:82:in `run_callbacks' 
paperclip (4.1.1) lib/paperclip/callbacks.rb:36:in `run_paperclip_callbacks' 
paperclip (4.1.1) lib/paperclip/attachment.rb:442:in `post_process' 
paperclip (4.1.1) lib/paperclip/attachment.rb:114:in `assign' 
paperclip (4.1.1) lib/paperclip/has_attached_file.rb:66:in `block in define_setter' 
activerecord (4.1.0.beta1) lib/active_record/attribute_assignment.rb:45:in `_assign_attribute' 
activerecord (4.1.0.beta1) lib/active_record/attribute_assignment.rb:32:in `block in assign_attributes' 

这是我的模型: - / model / asset.rb

class Asset < ActiveRecord::Base
  ...

  belongs_to :user
  belongs_to :member_visibility

  has_attached_file :image,
                     :processors => [:watermark], 
                     :url => "/system/:class/:attachment/:id_partition/:style/:filename",
                     :path => ":rails_root/public/system/:class/:attachment/:id_partition/:style/:filename",
                              :styles => { 
                                :seperator => {
                                    :geometry => '2000x1333#',
                                    :format => 'jpg'
                                  },   

                                :thumb => Proc.new { |instance| instance.resize_cover_image('thumb') },
                                :gallery => { 
                                  :processors => [:watermark],
                                  :geometry => Proc.new { |instance| instance.resize_cover_image('gallery') }, 
                                  :watermark_path => Rails.root.join('app/assets/images/watermark.png'), 
                                  :position => 'SouthWest',
                                  :format => 'jpg'},
                                 :medium => {
                                  :processors => [:watermark],
                                  :geometry => Proc.new { |instance| instance.resize_cover_image('medium') }, 
                                  :watermark_path => Rails.root.join('app/assets/images/watermarksmall.png'), 
                                  :position => 'SouthEast',
                                  :format => 'jpg',
                                  :quality => 80
                                 }
                                }, 
                                :convert_options => {
                                  :seperator    => '-set colorspace sRGB -strip -quality 50 -sharpen 0x0.5',
                                  :thumb  => '-set colorspace sRGB -strip -quality 80',
                                  :gallery    => '-set colorspace sRGB -strip -quality 90',
                                  :medium   => '-set colorspace sRGB -strip -quality 80 -sharpen 0x0.5'
                                }, dependent: :allow_destroy

  validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/ 
  crop_attached_file :image, :aspect => :set_aspect_ratio


    def resize_cover_image(style)
      geo = Paperclip::Geometry.from_file(Paperclip.io_adapters.for(image))

      case style
      when 'thumb'
        geo.horizontal? ? '350x233#' : '200x300#'
      when 'medium'
        geo.horizontal? ? '750x500#' : '370x555#'
      when 'gallery'
        geo.horizontal? ? '1125x750#' : '600x900#'
      end
    end

 private

   ...
end

请注意,如果我注释掉了gallery&amp ;;的水印路径中等以及处理器工作得很好。所以我的回形针设置都很好。

这里是lib / paperclip_processors / watermark.rb(根据Watermark images with paperclip, rails 4

module Paperclip
  class Watermark < Processor
    # Handles watermarking of images that are uploaded.
    attr_accessor :current_geometry, :target_geometry, :format, :whiny, :convert_options, :watermark_path, :watermark_offset, :overlay, :position

    def initialize file, options = {}, attachment = nil
      super
      geometry = options[:geometry]
      @file = file
      @crop = geometry[-1,1] == '#'
      @target_geometry = Geometry.parse geometry
      @current_geometry = Geometry.from_file @file
      @convert_options = options[:convert_options]
      @whiny = options[:whiny].nil? ? true : options[:whiny]
      @format = options[:format]
      @watermark_path = options[:watermark_path]
      @position = options[:position].nil? ? "SouthEast" : options[:position]
      @watermark_offset = options[:watermark_offset]
      @overlay = options[:overlay].nil? ? true : false
      @current_format = File.extname(@file.path)
      @basename = File.basename(@file.path, @current_format)
    end

    # TODO: extend watermark

    # Returns true if the +target_geometry+ is meant to crop.
    def crop?
      @crop
    end

    # Returns true if the image is meant to make use of additional convert options.
    def convert_options?
      not @convert_options.blank?
    end

    # Performs the conversion of the +file+ into a watermark. Returns the Tempfile
    # that contains the new image.
    def make
      dst = Tempfile.new([@basename, @format].compact.join("."))
      dst.binmode

      if watermark_path
          command = "convert"
          params  = %W['#{fromfile}']
          params += transformation_command
          params += %W['#{watermark_path}' -gravity #{@position} -composite]
          params << "'#{tofile(dst)}'"
        else
          command = "convert"
          params = ["'#{fromfile}'"]
          params += transformation_command
          params << "'#{tofile(dst)}'"
      end

      begin
        Paperclip.run(command, params.join(' '))
      rescue ArgumentError, Cocaine::CommandLineError
        raise PaperclipError, "There was an error processing the watermark for #{@basename}" if @whiny
      end

      dst
    end

    def fromfile
      File.expand_path(@file.path)
    end

    def tofile(destination)
      File.expand_path(destination.path)
    end

    def transformation_command
      scale, crop = @current_geometry.transformation_to(@target_geometry, crop?)
      trans = %W[-resize '#{scale}']
      trans += %W[-crop '#{crop}' +repage] if crop
      trans << convert_options if convert_options?
      trans
    end
  end
end

---我注意到的其他事情:

如果我完全删除了make的内容 - 请将其更改为:

def make

我仍然得到错误。如果有人能够指出我正确的方向,那就太棒了!

干杯。

1 个答案:

答案 0 :(得分:0)

要修复它并使其正常工作,我必须删除:processors =&gt; [:watermark]来自:gallery and:medium one。

has_attached_file :image,
                     :processors => [:watermark], 
                     :url => "/system/:class/:attachment/:id_partition/:style/:filename",
                     :path => ":rails_root/public/system/:class/:attachment/:id_partition/:style/:filename",
                              :styles => { 
                                  :seperator => {
                                      :geometry => '2000x1333#',
                                      :format => 'jpg'
                                  },   

                                  :thumb => Proc.new { |instance| instance.resize_cover_image('thumb') },
                                  :gallery => { 
                                      :geometry => Proc.new { |instance| instance.resize_cover_image('gallery') }, 
                                      :watermark_path => Rails.root.join('app/assets/images/watermark.png'), 
                                      :position => 'SouthWest',
                                      :format => 'jpg'
                                   },
                                   :medium => {
                                      :geometry => Proc.new { |instance| instance.resize_cover_image('medium') }, 
                                      :watermark_path => Rails.root.join('app/assets/images/watermarksmall.png'), 
                                      :position => 'SouthEast',
                                      :format => 'jpg'
                                   }
                                  },
                               :convert_options => {
                                    :gallery    => '-set colorspace sRGB -strip -quality 80 -sharpen 0x0.5',
                                    :thumb  => '-set colorspace sRGB -strip -quality 80',
                                    :medium   => '-set colorspace sRGB -strip -quality 80',
                                    :seperator   => '-set colorspace sRGB -strip -quality 50 -sharpen 0x0.5'
                               }, dependent: :allow_destroy