__struct __ / 1未定义,无法展开struct Gazette.User

时间:2018-02-08 15:04:26

标签: elixir phoenix-framework ecto mix

我正在构建凤凰 1.3 应用,但是在 1.2 Pheonix教程之后,我运行了mix phoenix.gen.model等已弃用的命令

我不确定它是否已链接,但现在我收到此错误:

== Compilation error in file lib/gazette_web/controllers/user_controller.ex ==
** (CompileError) lib/gazette_web/controllers/user_controller.ex:12: Gazette.User.__struct__/1 is undefined, cannot expand struct Gazette.User
    lib/gazette_web/controllers/user_controller.ex:11: (module)
    (stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
    (elixir) lib/kernel/parallel_compiler.ex:121: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/1

这是我的user_controller.ex文件:

defmodule Gazette.UserController do
  use GazetteWeb, :controller

  alias Gazette.User

  def show(conn, %{"id" => id}) do
    user = Repo.get!(User,id)
    render(conn, "show.html", user: user)
  end

  def new(conn, _params) do
    changeset = User.changeset(%User{})
    render conn, "new.html", changeset: changeset
  end

  def create(conn, %{"user" => user_params}) do
    # here will be an implementation
  end
end

这是我的model / user.ex文件:

defmodule Gazette.User do
  use Gazette.Web, :model

  schema "users" do
    field :email, :string
    field :name, :string
    field :password, :string, virtual: true
    field :password_hash, :string
    field :is_admin, :string
    field :is_writer, :string

    has_many :posts, Gazette.Post

    timestamps()
  end

  @required_fields ~w(email name)a
  @optional_fields ~w(is_admin is_writer)a

  @doc """
  Builds a changeset based on the `struct` and `params`.
  """
  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, @required_fields ++ @optional_fields)
    |> validate_required(@required_fields)
  end
end

任何想法?

1 个答案:

答案 0 :(得分:3)

您的public class CustomersController : ApiController { [Route("api/v2/customers", Order = 2)] [ResponseType(typeof(CustomerCollection))] public IHttpActionResult Get() { ... } [Route("api/v2/customers/{identifier}", Order = 1)] [ResponseType(typeof(Customer))] public IHttpActionResult Get(string identifier) { ... } } 是一个常规模块,它不会自动转向结构。要制作Gazette.User架构,您需要:

Ecto

我不确定use Ecto.Schema 做了什么,但我相信它已被弃用或其他什么。在定义模式时,最好是明确的。

您可能还需要/需要:

use Gazette.Web, :model

此代码可能已使用旧import Ecto.Changeset import Ecto.Query 任务自动生成。相反,您希望使用mix phx.gen.model来获取正确的样板。