我正在尝试在Spree商业中循环创建多个图像。狂欢版本:3.7.3,Rails 5.2,Ruby 2.5
我尝试通过控制台创建图像,并且效果很好。但是当我循环尝试它们时,会发生异常。 我也在development.rb中做了config.active_job.queue_adapter =:inline 和 self.inheritance_column =在images_decorator中为零,但没有运气
product = Spree::Product.last
images = ["https://www.gstatic.com/webp/gallery3/1.png","https://www.gstatic.com/webp/gallery3/2.png"]
images.each do |src|
puts("\nImage url: #{src}\n")
request = Typhoeus.get(src)
ext = file_extention(src)
file = Tempfile.new(['temp', ext], encoding: 'ascii-8bit')
File.open(file, 'wb') { |fp| fp.write(request.response_body) }
filename = src.gsub("https://www.gstatic.com/webp/gallery3/", "")
img = product.master.images.find_by(attachment_file_name: filename)
if img.blank?
puts "\n***Creating Image***\n"
product.master.images.create!(attachment: { io: file, filename: filename, content_type: get_content_type(src) },
attachment_file_name: filename)
# img = Spree::Image.create!(attachment: { io: file, filename: filename, :content_type => get_content_type(src) },
# attachment_file_name: filename)
# product.master.images << img (It was giving error here)
end
file.close
file.unlink
end
实用方法:
def get_content_type(url)
MIME::Types.type_for(url).first.try(:content_type)
end
def file_extention(url)
ext = MIME::Types.type_for(url).first.try(:content_type).split('/')[1]
".#{ext}"
end
期望创建图像但得到错误:ActiveRecord :: SubclassNotFound(无效的单表继承类型:Spree :: Image不是Spree :: Asset的子类)