Skip to content

Instantly share code, notes, and snippets.

@RichMorin
Last active November 20, 2018 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichMorin/5a8e370394c6b3246057456f3c0f065d to your computer and use it in GitHub Desktop.
Save RichMorin/5a8e370394c6b3246057456f3c0f065d to your computer and use it in GitHub Desktop.
concatenating the output of the link helper
I'm having trouble concatenating the results of the link() helper.
By way of context, here is the use case:
<%
import PhxHttpWeb.LayoutView
import RefData.Common
address = @item_data.address
types = ~w[ document email phone postal related web_site ]a
%>
<%= for type <- types do %>
<%= display(type, address) %>
<% end %>
Now, here is the (broken) code. FYI, keyss/1 returns a sorted list of keys
defmodule PhxHttpWeb.ResourcesView do
use PhxHttpWeb, :view
import RefData.Common
def display(type, address), do: display_h(type, address[type])
defp display_h(_, nil), do: ""
defp display_h(:web_site, submap) do
link_f = fn key ->
"<li>#{ link(key, to: submap[key]) }</li>"
end
links = submap
|> keyss()
|> Enum.map(link_f)
|> Enum.join("\n")
"""
<h3>Web Site</h3>
<ul>
#{ links }
</ul>
"""
end
...
end
Here is the nastygram I'm getting:
Protocol.UndefinedError at GET /resources/item
protocol String.Chars not implemented for {:safe,
[60, "a", [[32, "href", 61, 34, "https://anovaculinary.com/", 34]],
62, "main", 60, 47, "a", 62]}. This protocol is implemented for:
Atom, BitString, Date, DateTime, Float, Integer, List, NaiveDateTime,
Time, URI, Version, Version.Requirement
Apparently, link() emits a {:safe, <io_list>} tuple, as opposed to a string.
I'm not at all sure how to get around this issue.
-r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment