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:
[#<Tempfile:/var/folders/9n/gymcxgn13cjfgs1m8qj41z300000gn/T/download-20210115-4214-1rvb28j (closed)>, 200, ...]
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) %>
Any idea? Thank you.