Bit*_*ise 15 struct elixir phoenix-framework
我正在尝试在屏幕上显示一个表单.但是当我尝试启动服务器时,我一直收到此错误.locations_controller.ex ==
** (CompileError) web/controllers/locations_controller.ex:5: Locations.__struct__/1 is undefined, cannot expand struct Locations.顺便说一下,我是elixir的新手,所以我可能做了一些非常明显错误的事情.
这是我的代码:
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
Run Code Online (Sandbox Code Playgroud)
<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 %>
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
就像我说我收到这个错误:
locations_controller.ex ==
** (CompileError) web/controllers/locations_controller.ex:5: Locations.__struct__/1 is undefined, cannot expand struct Locations
Run Code Online (Sandbox Code Playgroud)
Dog*_*ert 20
Elixir中的模块需要以其全名或其名称来引用alias.您可以将全部更改Locations为AwesomeLunch.Locations,或者如果要使用较短的名称,则可以调用alias该模块:
defmodule AwesomeLunch.LocationsController do
alias AwesomeLunch.Locations
...
end
Run Code Online (Sandbox Code Playgroud)