gpt4 book ai didi

struct - 无法扩展结构 - Elixir /Phoenix

转载 作者:行者123 更新时间:2023-12-03 20:22:35 26 4
gpt4 key购买 nike

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

这是我的代码:

位置.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

看法

<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 来引用。其中。您可以更改所有 LocationsAwesomeLunch.Locations ,或者如果您想使用更短的名称,您可以调用 alias在该模块中:

defmodule AwesomeLunch.LocationsController do
alias AwesomeLunch.Locations

...
end

关于struct - 无法扩展结构 - Elixir /Phoenix ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40950619/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com