这是一个棘手的问题,我希望有人能帮助我。
我有一个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/