0

我一直在處理一個問題,其中出現錯誤:「沒有將字符串隱式轉換爲數組」。沒有將字符串隱式轉換爲數組 - Rails 4.1.0.beta1

把這個放在上下文中:我在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

​​

注意,如果我註釋掉畫廊&介質的水印路徑以及作爲處理器它工作得很好。所以我的回形針設置都很好。

這裏的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 

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

如果我卸妝的內容完全 - 因此將其更改爲:

DEF使

我仍然得到錯誤。如果任何人都可以將我指向正確的方向,那就太棒了!

乾杯。

+0

params =%W ['#{fromfile}'] - 您可以粘貼fromfile的輸出嗎? – tebayoso

+0

[「'\」/ var/folders/9p/r1mdc4gn5d9gdqvtm1tcf3l80000gn/T/f3a61c06aea3a7b924f08cfb0c78c67220140619-74328-jh95u5 [0] \「'」] –

+0

這是來自fromfile或params的輸出嗎? – tebayoso

回答

0

要解決它並讓它工作,我必須從:gallery和:medium一箇中刪除:processors => [:watermark]。

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 
+0

好的。如果將標題更改爲更類似於以下內容的描述,將會很有幫助:在回形針中添加水印時出現字符串到數組的問題...爲將來的搜索提供幫助! – tebayoso

相關問題