Hi, There I would like to insert mutiple images to batch records in has_rich_text as blob. How can I do it, I know how to do it with simple csv file. but how can I do it with images.
There is what I did.
I upload image files. loop images and insert to record
There is error message shows:
undefined method `attachments’ for nil:NilClass
I think I need create new “content” before attach image Please help !!! Thanks
---- model ----
class Product < ApplicationRecord
has_rich_text :content
---- view -----
<%= form_with url: imports_products_path, method: :post do | form | %>
<%= form.file_field :images, accept: 'image/png,image/gif,image/jpeg,image/jpg', :multiple => true %>
<%= form.button "Upload Images", class: "btn-primary" %>
<% end %>
---- controller -----
def imports
images = params[:images]
return redirect_to suppliers_path, alert: "No file selected" unless images
images.each do |image|
@p = Product.new
@p.name = "UNKNOW"
@p.Supplier_id = 9999
@p.Category_id = 0
@p.content.body.attachments.create(image)
@p.save
end
end