A similar question has been asked but none of the solutions have worked for me. Im building an app in solidus and need to generate qr codes for each time a product is created. Im using the rqr gem and then trying to attach the generated rqrcode png to a product in Active Storage.
I am getting the following error whenever im trying to render the image.
Can't resolve image into URL: undefined method `attachment_url' for #<ActionView::Base:0x007fd104542310>
<%= image_tag(@product.qr_code) if @product.qr_code.attached? %>
When a product gets created I generate a qr_code in the model like this.
has_one_attached :qr_code
after_create :generate_qr
def generate_qr
qrcode = RQRCode::QRCode.new("localhost:3000/products/#{self.slug}")
# NOTE: showing with default options specified explicitly
png = qrcode.as_png(
bit_depth: 1,
border_modules: 4,
color_mode: ChunkyPNG::COLOR_GRAYSCALE,
color: "black",
file: nil,
fill: "white",
module_px_size: 6,
resize_exactly_to: false,
resize_gte_to: false,
size: 120
)
image_name = SecureRandom.hex
IO.binwrite("tmp/#{image_name}.png", png.to_s)
blob = ActiveStorage::Blob.create_after_upload!(
io: File.open("tmp/#{image_name}.png"),
filename: image_name,
content_type: 'png'
)
self.qr_code.attach(blob)
end
I've also tried attaching directly with the attach method
self.qr_code.attach(
io: File.open("tmp/#{image_name}.png"),
filename: image_name,
content_type: 'png'
)
When I run @product.qr_code.attached?
I get true
so I know the Active Record attaching is working.
I have checked all my all my config files and everything seems to be correct.
config/storage.yml
test:
service: Disk
root: <%= Rails.root.join("tmp/storage") %>
local:
service: Disk
root: <%= Rails.root.join("storage") %>
config.active_storage.service = :local <--- congif/enivronments/development.rb
config.active_storage.service = :local<--- congif/enivronments/production.rb
config.active_storage.service = :test <--- congif/enivronments/test.rb
Does anyone know what could be causing this error message? Thanks.