struct __ / 1未定义,无法扩展struct错误Phoenix 1.3

时间:2017-06-08 20:14:03

标签: phoenix-framework

我试图在凤凰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

1 个答案:

答案 0 :(得分:0)

关于这个

  

确定有道理。我已将其更新到控制器:changeset =   Contact.Email.changeset(%Contact.Email {})但我现在得到:警告:   函数Iotc.Contact.Email.changeset / 1是未定义的或私有的。难道   你的意思是:* changeset / 2

您只在电子邮件模块中定义了一个功能changeset/2

但你正在做Contact.Email.changeset(%Contact.Email{})只传递一个论点。做``Contact.Email.changeset(%Contact.Email {})`它应该可以工作。

签名的/2部分告诉您函数的 arity ,即需要多少参数。