protocol Phoenix.HTML.Safe not implemented Elixir Phoenix

送分小仙女□ 提交于 2019-12-23 13:21:13

问题


I have an object with such values as

%{"Friday" => [], "Monday" => [], "Saturday" => [], "Sunday" => ["3:0-4:0", "6:0-7:0"], "Thursday" => [], "Tuesday" => [], "Wednesday" => []}

I wanted to show it in my email template's view, i just stated as

<p>Schedule: <%= @schedule %></p>

I got this error on it

** (Protocol.UndefinedError) protocol Phoenix.HTML.Safe not implemented for %{"Friday" => [], "Monday" => [], "Saturday" => [], "Sunday" => ["3:0-4:0", "6:0-7:0"], "Thursday" => [], "Tuesday" => [], "Wednesday" => []}

What will be the best way to show it in HTML?


回答1:


You cannot directly output a Map like that; only things that implement the Phoenix.HTML.Safe protocol. If you want to print what iex would print (which is Elixir syntax if possible), you can explicitly call inspect to convert the Map into a String, and then output that:

<p>Schedule: <%= inspect @schedule %></p>

If you want to print it in a different way, you can use for:

<p>
  Schedule: 
  <%= for {key, value} <- @schedule do %>
    ...use key and value variables here...
  <% end %>
</p>


来源:https://stackoverflow.com/questions/40971719/protocol-phoenix-html-safe-not-implemented-elixir-phoenix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!