I’m stuck with the image display in a rails template.
I’m getting it from an API as Tempfile and already tried different ways, but still no success.
So in a controller the show action looks like this:
def show
@qrcode = QrcodeService.call(some_id)
end
The result I’m getting in the above action looks like that:
I tried to do that using a custom helper method defined in ApplicationHelper:
def qrcode_preview(qrcode)
if qrcode[1] == 200
"<img src='data:image/png;base64,#{Base64.encode64(qrcode.first.read)}'>".html_safe
else
'<b>Image not yet ready</b>'
end
end
and use in the view:
<%= qrcode_preview(@qrcode) %>
but it fails with:
ActionView::Template::Error (closed stream):
1: <h2 class="ml-4 text-2xl uppercase">Scan me to sign in</h2>
2: <%= qrcode_preview(@qrcode) %>
# application_helper.rb
def qrcode_preview(qrcode)
if qrcode[1] == 200
data = File.read(qrcode.first) # to get the Tempfile from the response array
"<img src='data:image/png;base64,#{Base64.encode64(data)}'>".html_safe
else
'<b>Image not yet ready</b>'
end
end