未定义的方法`sub'用于#<array:0x007fcdb436ad38> Jcrop with rails&amp; mongodb的</数组:0x007fcdb436ad38>

时间:2014-03-03 10:42:52

标签: ruby-on-rails mongoid

您好我正在使用带有Rails的MongoDB,因为我使用paperclip和Jcrop进行图像裁剪。 但是我收到了这个错误。请帮我。

undefined method `sub' for #<Array:0x007fcdb436ad38>
        crop_command + super.sub(/ -crop \S+/, '')

Model.rb

as_mongoid_attached_file :doctor_avatar,
:styles  => { :small => "100x100#", :large => "500x500>" }, 
:processors => [:cropper]
attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
after_update :reprocess_avatar, :if => :cropping?

def cropping?
     !crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
end

private

def reprocess_avatar
doctor_avatar.reprocess!
end

Cropper.rb

module Paperclip
class Cropper < Thumbnail
def transformation_command
if crop_command
     crop_command + super.first.sub(/ -crop \S+/, '')
else
     super
  end
end

def crop_command

target = @attachment.instance
    if target.cropping?
      " -crop '#{target.crop_w.to_i}x#{target.crop_h.to_i}+#{target.crop_x.to_i}+#{target.crop_y.to_i}'"
   end
  end
end

1 个答案:

答案 0 :(得分:1)

您对super的调用会返回一个数组,而不是字符串。但是#sub是在字符串上定义的方法。

我猜你可能想做:

crop_command + super.first.sub(/ -crop \S+/, '')