问题描述
我一直在处理出现错误的问题:没有将字符串隐式转换为数组".
I have been dealing with an issue where I get the error: "no implicit conversion of String into Array".
把这个放在上下文中:我在 Rails 4.1.0.beta1 上,我正在使用 Paperclip.我正在尝试为我的图像添加水印.我找到了以下解决方案:带有回形针、导轨 4 的水印图像
To put this in context: I am on Rails 4.1.0.beta1, and I am using Paperclip. I am trying to add a watermark to my images. I found the following solution: Watermark images with paperclip, rails 4
在收到此处所述的错误消息后,我昨晚在某个时候让它工作了 - 然后我做了一个小改动,但我已经撤消了.现在我再次收到以下错误?!这可能很容易,但我似乎没有看到它.
I sort of got it working at some point last night after receiving the here stated error message - then I made a slight change, which I had undone. And now I get the following error again?! It might be a easy one, but I just don't seem to see it.
任何帮助将不胜感激.
错误信息如下:
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
Here is my model: - /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 &的水印路径中等以及处理器它工作得很好.所以我的回形针设置都很好.
Note if I comment out the watermark path of gallery & medium as well as the processors it works just fine. So my paperclip setup is all good.
这里是 lib/paperclip_processors/watermark.rb(根据 带有回形针的水印图像,导轨 4)
Here the lib/paperclip_processors/watermark.rb (as per 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
---我注意到的其他事情:
--- Other things I noticed:
如果我完全删除 make 的内容 - 所以将其更改为:
If I remove the contents of make completely - so change it to:
定义制作
结束
我仍然收到错误消息.如果有人能指出我正确的方向,那就太棒了!
I still get the error. If anyone can point me into the right direction that would be awesome!
干杯.
推荐答案
为了修复它并让它工作,我不得不从 :gallery 和 :medium 中删除 :processors => [:watermark] .
To fix it and get it working I had to remove the :processors => [:watermark] from :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
这篇关于没有将字符串隐式转换为数组 - Rails 4.1.0.beta1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!