Error: undefined function user/2 when running mix phoenix.server
Error: undefined function user/2 when running mix phoenix.server
我创建了这个模块:
defmodule Discuss.Topic do
user Discuss.Web, :model
schema "topics" do
field :title, :string
end
def changeset(struct, params \ %{}) do
struct
|> cast(params, [:title])
|> validate_required([:title])
end
end
我在使用 mix phoenix.server
时遇到了这个错误
== Compilation error in file web/models/topic.ex ==
** (CompileError) web/models/topic.ex:2: undefined function user/2
(stdlib) erl_eval.erl:680: :erl_eval.do_apply/6
(elixir) lib/kernel/parallel_compiler.ex:229: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/7
知道发生了什么事吗?我是 Phoenix 和 Elixir 的新手。
user Discuss.Web, :model
^
|
here
应该是
use Discuss.Web, :model
更新
由于 "web" 应该是一个仅卸载 HTTP 和 WebSocket 的薄层,并将工作委托给底层业务逻辑,Phoenix 不再将模型视为 Web 层的一部分。因此,您应该 use Ecto.Schema
.
而不是 use Discuss.Web, :model
顺便说一下,Web 层现在只包含控制器、视图、通道和路由器。
我创建了这个模块:
defmodule Discuss.Topic do
user Discuss.Web, :model
schema "topics" do
field :title, :string
end
def changeset(struct, params \ %{}) do
struct
|> cast(params, [:title])
|> validate_required([:title])
end
end
我在使用 mix phoenix.server
== Compilation error in file web/models/topic.ex ==
** (CompileError) web/models/topic.ex:2: undefined function user/2
(stdlib) erl_eval.erl:680: :erl_eval.do_apply/6
(elixir) lib/kernel/parallel_compiler.ex:229: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/7
知道发生了什么事吗?我是 Phoenix 和 Elixir 的新手。
user Discuss.Web, :model
^
|
here
应该是
use Discuss.Web, :model
更新
由于 "web" 应该是一个仅卸载 HTTP 和 WebSocket 的薄层,并将工作委托给底层业务逻辑,Phoenix 不再将模型视为 Web 层的一部分。因此,您应该 use Ecto.Schema
.
use Discuss.Web, :model
顺便说一下,Web 层现在只包含控制器、视图、通道和路由器。