I’m having trouble rendering an Ecto.Enum array field in liveview.
Here’s the offending heex line:
<:item title="Services offered"><%= @facility.services_offered %></:item>
and here’s the line in my schema:
field :services_offered, {:array, Ecto.Enum}, values: [:dance, :swim, :yoga]
and this is the error it gives me:
** (ArgumentError) lists in Phoenix.HTML and templates may only contain integers representing bytes, binaries or other lists, got invalid entry: :dance
stacktrace:
(phoenix_html 3.3.1) lib/phoenix_html/safe.ex:81: Phoenix.HTML.Safe.List.to_iodata/1
(phoenix_html 3.3.1) lib/phoenix_html/safe.ex:49: Phoenix.HTML.Safe.List.to_iodata/1
(parity 0.1.0) lib/myapp_web/live/facility_live/index.ex:68: anonymous fn/3 in MyAppWeb.FacilityLive.Index.render/1
Any pointers here? Googling has gotten me nowhere.
You can’t render a list of atoms. You should convert it to something representable. For example:
<:item title="Services offered"><%= Enum.map_join(@facility.services_offered, ", ", &to_string/1) %></:item>
or You can try this…
<:item title="Services offered"><%= inspect @facility.services_offered %></:item>
I get that, but it seems like LiveView automatically renders atoms from enums correctly, as I have a single-select enum type already and it renders fine (without me casting all of the enums to string) It seems like maybe this should be fixed in Phoenix.Safe
?
The problem is not on Phoenix. When you try to interpolate a term, Kernel.to_string/1
is called on the term. The implementation for atom is simply Atom.to_string/1
. The implementation for lists raises the error you’re seeing: elixir/list.ex at v1.14.4 · elixir-lang/elixir · GitHub.
It’s happening in Phoenix.Html.Safe
: phoenix_html/safe.ex at main · phoenixframework/phoenix_html · GitHub
I think maybe it could have a line like
def to_iodata(h) when is_atom(h) do:
Phoenix.HTML.Engine.html_escape(Atom.to_string(atom))
end
but I’m super new to phoenix, so I could be way off!
1 Like
You’re actually right, my bad 
But the reasoning is the same from List.to_string/1, I guess.
I‘d expect lists are not supported by design. The implementation for lists seems to be there to support the structure of iodata/iolists, rather than lists of user provided values.