Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 保存前使用回形针图像_Ruby On Rails 3_Callback_Paperclip_Base64 - Fatal编程技术网

Ruby on rails 3 保存前使用回形针图像

Ruby on rails 3 保存前使用回形针图像,ruby-on-rails-3,callback,paperclip,base64,Ruby On Rails 3,Callback,Paperclip,Base64,我正在Rails中使用回形针保存图像上传,效果很好 has_attached_file :image, :styles => { :small => "80x90#" } 然后,我希望在创建模型时,将小图像的副本保存为模型中的base64编码字符串 before_update :encode_image private def encode_image self.base64 = ActiveSupport::Ba

我正在Rails中使用回形针保存图像上传,效果很好

  has_attached_file :image, :styles => {
    :small => "80x90#"
  }
然后,我希望在创建模型时,将小图像的副本保存为模型中的base64编码字符串

  before_update :encode_image
  
  private
  
  def encode_image
    self.base64 = ActiveSupport::Base64.encode64(open(self.image.path(:small)).to_a.join)
  end
如果先前已保存图像,则上述代码在更新时起作用。我希望将此逻辑应用于在模型保存之前但在图像处理之后触发的回调

我原以为经过后处理后的
会是我的救世主,但在这一点上路径还没有完全成形(缺少id)

我错过了什么

丰富的

变通办法
我的解决方法是执行以下操作,但每次更新模型时都对例程进行编码似乎是一种耻辱:

  after_save :encode_image
  
  private
  
  def encode_image
    unless self.image.path(:small).blank?
      b64 = ActiveSupport::Base64.encode64(open(self.image.path(:small)).to_a.join)
      unless self.base64 == b64
        self.update_attribute :base64, b64
      end
    end
  end
  after_save :encode_image

  private

  def encode_image
    unless self.image.path(:small).blank?
      b64 = ActiveSupport::Base64.encode64(open(self.image.path(:small)).to_a.join)
      unless self.base64 == b64
        self.update_attribute :base64, b64
      end
    end
  end

我的解决方法是执行以下操作,但每次更新模型时都对例程进行编码似乎是一种耻辱:

  after_save :encode_image
  
  private
  
  def encode_image
    unless self.image.path(:small).blank?
      b64 = ActiveSupport::Base64.encode64(open(self.image.path(:small)).to_a.join)
      unless self.base64 == b64
        self.update_attribute :base64, b64
      end
    end
  end
  after_save :encode_image

  private

  def encode_image
    unless self.image.path(:small).blank?
      b64 = ActiveSupport::Base64.encode64(open(self.image.path(:small)).to_a.join)
      unless self.base64 == b64
        self.update_attribute :base64, b64
      end
    end
  end

感谢分享您的解决方案。我最后也做了同样的事情,但是如果你把条件放在回调本身上,你可以稍微清理一下代码,例如,在保存之后:encode_image,:除非=>proc{{记录}记录.image.path(:small.blank?}