当我这样做时(在iex -S mix
中):
alias Sling.Accounts.User
User.changeset(%User{},%{})
工作正常,但是当我添加以下行时: User.registration_changeset(%User {},%{})
我得到:
**(UndefinedFunctionError)函数Sling.Accounts.User.registration_changeset / 2是未定义的或私有的
(吊索)Sling.Accounts.User.registration_changeset(%Sling.Accounts.User {__ meta__:#Ecto.Schema.Metadata <:built,“ users”>,电子邮件:无,id:无,insert_at:无,password_hash:无,updated_at:无,用户名:nil},%{})
我在做什么错了?
下面的用户模型:
defmodule Sling.User do
use Sling.Web, :model
schema "users" do
field :username, :string
field :email, :string
field :password_hash, :string
field :password, :string, virtual: true
timestamps()
end
@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:username, :email])
|> validate_required([:username, :email])
|> unique_constraint(:username)
|> unique_constraint(:email)
end
def registration_changeset(struct, params) do
struct
|> changeset(params)
|> cast(params, [:password]
|> validate_length(:password, min: 6, max: 100)
|> put_password_hash()
end
defp put_password_hash(changeset) do
case changeset do
%Ecto.Changeset{valid?: true, changes: %{password: password}} ->
put_change(changeset, :password_hash, ComeoninBcrypt.hashpwsalt(password))
_ ->
changeset
end
end
end