I am trying to implement user authentication with the Phoenix web framework on Elixir but stumped by this error.
If I run changeset = Blog.User.changeset(%Blog.User{})
inside iex
I get the following error:
** (ArgumentError) unknown field "email" for changeset on
%Blog.User{__meta__: #Ecto.Schema.Metadata<:built, "users">, email:
nil, id: nil, inserted_at: nil, is_admin: false, name: nil, password:
nil, password_hash: nil, posts: #Ecto.Association.NotLoaded<association
:posts is not loaded>, updated_at: nil}
(ecto) lib/ecto/changeset.ex:1318: Ecto.Changeset.ensure_field_exists!/2
(ecto) lib/ecto/changeset.ex:1305: anonymous fn/5 in Ecto.Changeset.validate_required/3
(elixir) lib/enum.ex:1755: Enum."-reduce/3-lists^foldl/2-0-"/3
(ecto) lib/ecto/changeset.ex:1304: Ecto.Changeset.validate_required/3
Here is my model web/models/user.ex
:
defmodule Blog.User do
use Blog.Web, :model
schema "users" do
field :email, :string
field :name, :string
field :password, :string, virtual: true
field :password_hash, :string
field :is_admin, :boolean, default: false
has_many :posts, Blog.Post
timestamps()
end
@required_fields ~w(email)
@optional_fields ~w(name is_admin)
def changeset(struct, params \\ %{}) do
struct
|> cast(params, @required_fields ++ @optional_fields)
|> validate_required(@required_fields)
end
end
@required_fields
and@optional_fields
are lists of strings and perhapscast
orvalidate_required
require a list of atoms. – NoDisplayName