Use freshly generated non static file in template

I generate a qr code (that I plan to use in the template) on fly in my controller.

def rapport(conn, _params) do
  {:ok, response} = HTTPoison.get 'http://localhost:8080/quiz/filter/1/count/2'
  quizes = process_response(response)
  Enum.each(quizes, & Qr.create_qr_code(@qr_folder <> &1.unique_id))
  render(conn, "quiz.html", quizes: quizes)
end

The mentioned template looks as:

<%= for quiz <- @quizes do %>
  <div class="page-break">
    <img src="http://localhost:4000/qr/<%= quiz.unique_id %>" />
    <ul>
      <%= for qa <- quiz.question_answers do %>
        <%= qa.question %>
        <ul>
          <%= for answer <- qa.answers do %>
            <li>
              <%= answer %>
            </li>
          <% end %>
        </ul>
      <% end %>
    </ul>
  </div>
<% end %>

The problem is, that freshly generated qr code is not displayed in the rendered page. However, if I replace

<img src="http://localhost:4000/qr/<%= quiz.unique_id %>" />

with

<img src='http://localhost:4000/qr/09a588dcfeaa4d2a8fb45d290a6ba6d1' />

(09a588dcfeaa4d2a8fb45d290a6ba6d1 is an image generated in previous try) than qr code is shown.

Any idea what could be wrong?

OK, figured out by myself. The problem is the png library doesn’t close the file. Thus, the needed file wasn’t ready at the time.

1 Like