I am building a Phoenix 1.3 app but following a 1.2 Pheonix tutorial, I ran deprecated commands like mix phoenix.gen.model
I am not sure if it is linked but now I get this error:
== Compilation error in file lib/gazette_web/controllers/user_controller.ex ==
** (CompileError) lib/gazette_web/controllers/user_controller.ex:12: Gazette.User.__struct__/1 is undefined, cannot expand struct Gazette.User
lib/gazette_web/controllers/user_controller.ex:11: (module)
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
(elixir) lib/kernel/parallel_compiler.ex:121: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/1
Here is my user_controller.ex file:
defmodule Gazette.UserController do
use GazetteWeb, :controller
alias Gazette.User
def show(conn, %{"id" => id}) do
user = Repo.get!(User,id)
render(conn, "show.html", user: user)
end
def new(conn, _params) do
changeset = User.changeset(%User{})
render conn, "new.html", changeset: changeset
end
def create(conn, %{"user" => user_params}) do
# here will be an implementation
end
end
and this is my model/user.ex file:
defmodule Gazette.User do
use Gazette.Web, :model
schema "users" do
field :email, :string
field :name, :string
field :password, :string, virtual: true
field :password_hash, :string
field :is_admin, :string
field :is_writer, :string
has_many :posts, Gazette.Post
timestamps()
end
@required_fields ~w(email name)a
@optional_fields ~w(is_admin is_writer)a
@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct, params \\ %{}) do
struct
|> cast(params, @required_fields ++ @optional_fields)
|> validate_required(@required_fields)
end
end
any idea ?
Your Gazette.User
is a regular module, it won’t be turned to the struct automagically. To make an Ecto
schema out of it, you need to:
use Ecto.Schema
I am unsure what use Gazette.Web, :model
does, but I believe it’s deprecated or something. It’s always better to be explicit when defining schemas.
You might also want/need to:
import Ecto.Changeset
import Ecto.Query
This code may have been autogenerated using the old mix phx.gen.model
task. Instead, you want to use mix phx.gen.schema
in order to get the correct boilerplate.