[英]Cannot expand struct - elixir/phoenix
我正在嘗試在屏幕上顯示表單。 但是,當我嘗試啟動服務器時,仍然出現此錯誤。 locations_controller.ex == ** (CompileError) web/controllers/locations_controller.ex:5: Locations.__struct__/1 is undefined, cannot expand struct Locations
。 順便說一句,我是長生不老葯,所以我可能正在做一些明顯不對的事情。
這是我的代碼:
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
<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
Elixir中的模塊需要以其全名或alias
來引用。 您可以將所有Locations
都更改為AwesomeLunch.Locations
,或者如果您想使用更短的名稱,則可以在該模塊中調用alias
:
defmodule AwesomeLunch.LocationsController do
alias AwesomeLunch.Locations
...
end
我正在開發一個傘式項目,有時會遇到相同的錯誤。
如果創建在App1
聲明的結構,並想在App2
使用它,則必須將App1
作為依賴項添加到App2
中。 如果您不這樣做,並且在App1
之前加載了App2
,則會發生錯誤。
示例: {:app1, in_umbrella: true}
我遇到了同樣的錯誤,對我來說,它可以通過以下方式配置控制器:
defmodule AwesomeLunch.LocationsController do
use AwesomeLunch.Web, :controller
alias AwesomeLunch.Locations
...
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.