首页 > 解决方案 > Phoenix Schema 在表单选择中分配了 throw Phoenix.HTML.Safe not implemented 错误

问题描述

我收到以下错误:

protocol Phoenix.HTML.Safe not implemented for {"Bad Product, LLC"}

从以下代码:

审查控制器

def new(conn, _params) do # creating a review, the Company is a schema association
    changeset = Accounts.change_review(%Review{})
    companies = Repo.all from c in Company, select: {c.name} 
    render(conn, "new.html", changeset: changeset, companies: companies)
end

模板:

<%= select f, :company_id, @companies %>

通过对 SO 的研究,我尝试添加inspect

<%= select f, :company_id, inspect @companies %>

但这会引发以下错误:

protocol Enumerable not implemented for "[{\"Bad Product, LLC\"}]"

看起来它正试图按预期逃避它,所以我重构了控制器以枚举公司:

render(conn, "new.html", changeset: changeset, companies: Accounts.list_companies() |> Enum.map(&{&1.name}))

但它仍然会引发Enumerable not implemented错误。

谢谢!

标签: elixirphoenix-framework

解决方案


公司 = Repo.all from c in Company,选择:{c.name}

在这里您只选择名称

而您正在尝试访问 company_id

尝试

公司 = Repo.all(公司)


推荐阅读