协议Phoenix.HTML.Safe未实现Elixir Phoenix [英] protocol Phoenix.HTML.Safe not implemented Elixir Phoenix

查看:39
本文介绍了协议Phoenix.HTML.Safe未实现Elixir Phoenix的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个值为

%{"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>

我遇到了这个错误

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

用HTML展示它的最佳方法是什么?

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

推荐答案

您不能直接输出这样的Map;只有实现Phoenix.HTML.Safe协议的内容.如果要打印iex将要打印的内容(如果可能,则使用Elixir语法),可以显式调用inspect将Map转换为String,然后输出:

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>

如果要以其他方式打印,可以使用for:

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>

这篇关于协议Phoenix.HTML.Safe未实现Elixir Phoenix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆