Saving generated Image with Carrierwave - no form, all inside the model
this is a tricky one, I hope someone can help me.
I have a article_set, which has many set_items, each set item has an image. I want to create an collage of the images, so one image out of many, and save it with carrierwave.
that's whats going on in my model:
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}_#{开发者_JAVA百科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
So far, so good. I can save the set and it runs though this method, but I guess the last part
#save and upload to s3
self.blog_image.store!(template)
self.blog_image = self.blog_image.store_path
self.write_carrierwave_identifier
self.save
is probably incorrect, in my database I get only NULL values for blog_image. It should, of course, contain the file name of the generated file... any help highly appreciated.
thanks
That 3rd last line looks a bit dodgy:
self.blog_image = self.blog_image.store_path
If you remove that i'm fairly sure it should work.
精彩评论