【发布时间】:2017-11-23 02:25:07
【问题描述】:
我正在尝试在 phoenix 1.3 应用中创建联系表单。我使用mix phx.gen.html 创建相关文件。但是,尝试启动服务器时出现编译错误:
== Compilation error on file lib/iotc/web/controllers/email_controller.ex ==
** (CompileError) lib/iotc/web/controllers/email_controller.ex:7: Email.__struct__/1 is undefined, cannot expand struct Email
(stdlib) lists.erl:1354: :lists.mapfoldl/3
lib/iotc/web/controllers/email_controller.ex:6: (module)
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
查看其他一些有类似问题的帖子,可能与别名有关,但我在控制器中有alias Itoc.Contact,我认为alias Iotc.Contact.Email 不会在这里。
email_controller.ex
defmodule Iotc.Web.EmailController do
use Iotc.Web, :controller
alias Iotc.Contact
def index(conn, _params) do
changeset = Email.changeset(%Email{})
emails = Contact.list_emails()
render(conn, "index.html", emails: emails, changeset: changeset)
end
...
email.ex
defmodule Iotc.Contact.Email do
use Ecto.Schema
import Ecto.Changeset
alias Iotc.Contact.Email
schema "contact_emails" do
field :email, :string
field :event, :string
field :message, :string
field :name, :string
timestamps()
end
@doc false
def changeset(%Email{} = email, attrs) do
email
|> cast(attrs, [:name, :email, :message, :event])
|> validate_required([:name, :email, :message, :event])
end
end
【问题讨论】:
-
如果您想使用名称
Email,您确实需要该别名。如果您只想为Contact模块起别名,也可以切换到Contact.Email。 -
OK 是有道理的。我已将控制器更新为:
changeset = Contact.Email.changeset(%Contact.Email{})但我现在得到:warning: function Iotc.Contact.Email.changeset/1 is undefined or private. Did you mean one of: * changeset/2