无法扩展struct - elixir / phoenix

时间:2016-12-03 17:39:01

标签: struct elixir phoenix-framework

我试图在屏幕上显示表单。但是当我尝试启动服务器时,我一直收到此错误。 locations_controller.ex == ** (CompileError) web/controllers/locations_controller.ex:5: Locations.__struct__/1 is undefined, cannot expand struct Locations。顺便说一句,我是elixir的新手,所以我可能做了一些非常明显的错误。

这是我的代码:

locations.controller.ex

 def new(conn, _params) do
    changeset = Locations.changeset(%Locations{})

    render conn, "new.html", changeset: changeset
  end

  def create(conn, %{"locations" => %{ "start" => start, "end" => finish }}) do
    changeset = %AwesomeLunch.Locations{start: start, end: finish}
    Repo.insert(changeset)

    redirect conn, to: locations_path(conn, :index)
  end

VIEW

<h1>Hey There</h1>

<%= form_for @changeset, locations_path(@conn, :create), fn f -> %>

  <label>
    Start: <%= text_input f, :start %>
  </label>

  <label>
    End: <%= text_input f, :end %>
  </label>

  <%= submit "Pick An Awesome Lunch" %>

<% end %>

模型

    defmodule AwesomeLunch.Locations do
  use AwesomeLunch.Web, :model

  use Ecto.Schema
  import Ecto.Changeset

  schema "locations" do
    field :start
    field :end
  end

  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, [:start, :end])
    |> validate_required([:start, :end])
  end
end

就像我说的那样,我收到了这个错误:

    locations_controller.ex ==
** (CompileError) web/controllers/locations_controller.ex:5: Locations.__struct__/1 is undefined, cannot expand struct Locations

2 个答案:

答案 0 :(得分:0)

我遇到了同样的错误,对我而言,它以这种方式配置控制器:

defmodule AwesomeLunch.LocationsController do
  use AwesomeLunch.Web, :controller

  alias AwesomeLunch.Locations

  ...
end

答案 1 :(得分:0)

我正在开发一个总体项目,有时会遇到相同的错误。

如果创建在App1中声明的结构,并想在App2中使用它,则必须将App1添加到App2作为依赖。如果您不这样做,并且在App2之前加载了App1,则会发生错误。