我正在按照Programming Phoenix 1.4
书构建一个简单的应用程序。
添加逻辑以创建新用户时,出现此错误:
== Compilation error in file lib/rumbl_web/controllers/user_controller.ex ==
** (CompileError) lib/rumbl_web/controllers/user_controller.ex:19: Rubml.Accounts.User.__struct__/1 is undefined, cannot expand struct Rubml.Accounts.User. Make sure the struct name is correct. If the struct name exists and is correct but it still cannot be found, you likely have cyclic module usage in your code
(stdlib 3.12.1) lists.erl:1354: :lists.mapfoldl/3
lib/rumbl_web/controllers/user_controller.ex:18: (module)
(stdlib 3.12.1) erl_eval.erl:680: :erl_eval.do_apply/6
我已经仔细检查过,不认为我没有错过任何内容,也没有错别字。
这里是我的控制器:
defmodule RumblWeb.UserController do
use RumblWeb, :controller
alias Rumbl.Accounts
alias Rubml.Accounts.User
def index(conn, _params) do
users = Accounts.list_users()
# IO.puts users
render(conn, "index.html", users: users)
end
def show(conn, %{"id" => id}) do
user = Accounts.get_user(id)
render(conn, "show.html", user: user)
end
def new(conn, _params) do
changeset = Accounts.change_user(%User{})
render(conn, "new.html", changeset: changeset)
end
end
和用户模型:
defmodule Rumbl.Accounts.User do
use Ecto.Schema
import Ecto.Changeset
schema "users" do
field :name, :string
field :username, :string
timestamps()
end
def changeset(user, attrs) do
user
|> cast(attrs, [:name, :username])
|> validate_required([:name, :username])
|> validate_length(:username, min: 1, max: 20)
end
end
如果有人想浏览,这里是回购链接:https://github.com/niranjans/rumbl