Hi @grangerelixx - it wasn’t totally clear to me what you are asking, but to show an image in heex would be something like:
<%= for post <- @posts do %>
<tr>
<td><img src={post.img_url} /></td>
<td><%= authors_list(post.id) %></td>
</tr>
<% end %>
I’m not sure what you’re asking in the second part of the question. Are you trying to lookup an image name for each string in the list and display it?
By the way it might be interesting to know which version of Phoenix. As there are some changes recently
You put the images in priv/static? Are You sure plug static is configured to serve these? It should be priv/static/images or …assets, depending on Phoenix version
How do You assign image url? => What is your way of linking these images to authors?
def authors_list(post_id) do
list =
Post.list_posts()
|> Enum.map(fn {p_id, author_name} ->
if p_id == post_id do
author_name
end
end)
|> Enum.reject(&(&1 == nil))
end
if list == [] do
"NONE"
else
list
|> Enum.sort()
|> Enum.join(", ")
end
I currently use phoenix 1.6.15. Yes I put under priv/static.
I am thinking of using a case block to map the images to authors as my dataset is very small now.
So you need to define a function that converts the string to the image URL. For example
def get_image_url(author_string) do
"/images/author_#{author_string}.png" # or whatever
end
Probably the best place is in the module that authors_list is defined.
Then you can use it in the heex:
<%= for post <- @posts do %>
<tr>
<td>
<%= for author_string <- authors_list(post.id) do %>
<img src={get_image_url(author_string)} />
<% end %>
</td>
</tr>
<% end %>`