ruby-on-rails - 使用 Carrierwave 保存生成的图像 - 没有形式,都在模型内部

标签 ruby-on-rails ruby ruby-on-rails-3 rmagick carrierwave

这是一个棘手的问题,我希望有人能帮助我。

我有一个article_set,里面有很多set_items,每个set_item都有一张图片。 我想创建图像的拼贴画,从多张图像中选择一张,然后用载波保存。

这就是我的模型中发生的事情:

require 'RMagick'

class ArticleSet < ActiveRecord::Base

    # ...

    after_create :create_collage  
    mount_uploader :blog_image, BlogImageUploader

    def create_collage
      # load the template
      template = Magick::Image.read("#{RAILS_ROOT}/public/images/set_template.png").first

      # go through all set items by z_index and add lay it over the template
      for item in self.set_items
        photo = Magick::Image.read(item.product.assets.first.image.url(:thumb).to_s).first
        photo.scale(item.width, item.height)
        # composite item over template offsetting pos_x and pos_y for the template border
        template.composite!(photo, item.pos_x, item.pos_y, Magick::OverCompositeOp)
      end

      # save composite image to JPG
      template.write("set_#{self.id}_#{Time.now.to_i}.jpg")

      #save and upload to s3

      self.blog_image.store!(template)
      self.blog_image = self.blog_image.store_path
      self.write_carrierwave_identifier
      self.save  

    end

end

到目前为止,还不错。我可以保存设置并通过此方法运行,但我猜是最后一部分

       #save and upload to s3

      self.blog_image.store!(template)
      self.blog_image = self.blog_image.store_path
      self.write_carrierwave_identifier
      self.save  

可能不正确,在我的数据库中,我只得到 blog_image 的 NULL 值。 当然,它应该包含生成文件的文件名...... 非常感谢任何帮助。

谢谢

最佳答案

倒数第三行看起来有点狡猾:

self.blog_image = self.blog_image.store_path

如果你删除它,我相当确定它应该可以工作。

关于ruby-on-rails - 使用 Carrierwave 保存生成的图像 - 没有形式,都在模型内部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6696195/

相关文章:

ruby-on-rails - 数组合并(联合)

ruby-on-rails - 为什么在部署 Rails 应用程序时需要 Apache 服务器?

ruby - Ruby2Ruby 与 ParseTree 兼容吗?

ruby-on-rails - 与没有 Ajax 或 jQuery on Rails 的情况类似/不同

ruby-on-rails - 如何使用 Active Storage Rails 上传到采用 AES256 加密(服务器端加密)的 AWS S3 存储桶?

ruby-on-rails - axlsx_rails gem : how to display leading zeros in Excel

ruby mailgun 发送邮件失败,出现 400 错误请求

ruby-on-rails-3 - 放 has_many :through into play and using it in an actual form

datetime - Rails 中单独的日期和时间表单字段

ruby-on-rails - 在哪里放置 Rails 种子数据