私は作業しているphoenix/phx v1.3プロジェクトを持っており、iex
セッションを使用してユーザーを作成しようとすると問題が発生しました。私が書いたコードには、アプリケーションがコンパイルされているので動作しているはずです。ectoとphoenix v1.3を使用してユーザーを作成する方法
以下のジェネレータを実行して、ユーザーアカウント用にschema
をセットアップしました。
mix phx.gen.json Accounts User users email:string encrypted_password:string username:string
私はその後
/lib/kegcopr_api/accounts/user.ex
user.ex
defmodule KegCopRAPI.Accounts.User do
use Ecto.Schema
# import Ecto
import Ecto.Changeset
# import Ecto.Query
schema "accounts_users" do
field :email, :string
field :encrypted_password, :string
field :username, :string
field :password, :string, virtual: true
timestamps()
end
@required_fields ~w(email username)
@optional_fields ~w()
def changeset(struct, params \\ :empty) do
struct
|> cast(params, @required_fields, @optional_fields)
|> validate_required(@required_fields, @optional_fields)
# |> cast(params, [:email, :encrypted_password, :username])
# |> validate_required([:email, :encrypted_password, :username])
|> validate_format(:email, ~r/@/)
|> validate_length(:username, min: 1, max: 20)
|> update_change(:email, &String.downcase/1)
|> unique_constraint(:email)
|> update_change(:username, &String.downcase/1)
|> unique_constraint(:username)
end
def registration_changeset(struct, params) do
struct
|> changeset(params)
|> cast(params, ~w(password), [])
|> validate_required(~w(password), [])
|> validate_length(:password, min: 6, max: 100) |> put_encrypted_pw
end
defp put_encrypted_pw(changeset) do
case changeset do
%Ecto.Changeset{valid?: true, changes: %{password: pass}} -> put_change(changeset, :encrypted_password, Comeonin.Bcrypt.hashpwsalt(pass))
_ ->
changeset
end
end
end
、セットアップにスキーマを変更することによって、ユーザーアカウントの作成を受け入れる方法についてのチュートリアルのカップルを通じて続きます次に、iex -S mix
を実行して、アプリケーションのインタラクティブなエリクシルセッションを開始し、以下のコマンドでDBにユーザーを挿入しようとしました。
changeset = KegCopRAPI.Accounts.User.registration_changeset(%KegCopRAPI.Accounts.User{}, %{email: "[email protected]", username: "first", password: "password"})
しかし、上記のコマンドをいただければ幸いです
warning: `Ecto.Changeset.cast/4` is deprecated, please use `cast/3` + `validate_required/3` instead
(kegcopr_api) lib/kegcopr_api/accounts/user.ex:21: KegCopRAPI.Accounts.User.changeset/2
(kegcopr_api) lib/kegcopr_api/accounts/user.ex:35: KegCopRAPI.Accounts.User.registration_changeset/2
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
(stdlib) erl_eval.erl:438: :erl_eval.expr/5
(elixir) src/elixir.erl:224: :elixir.erl_eval/3
(elixir) src/elixir.erl:212: :elixir.eval_forms/4
(iex) lib/iex/evaluator.ex:182: IEx.Evaluator.handle_eval/6
(iex) lib/iex/evaluator.ex:175: IEx.Evaluator.do_eval/4
(iex) lib/iex/evaluator.ex:155: IEx.Evaluator.eval/4
(iex) lib/iex/evaluator.ex:61: IEx.Evaluator.loop/3
(iex) lib/iex/evaluator.ex:21: IEx.Evaluator.init/4
(stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
** (ArgumentError) unknown field "email" for changeset on %KegCopRAPI.Accounts.User{__meta__: #Ecto.Schema.Metadata<:built, "accounts_users">, email: nil, encrypted_password: nil, id: nil, inserted_at: nil, password: nil, updated_at: nil, username: 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
(kegcopr_api) lib/kegcopr_api/accounts/user.ex:22: KegCopRAPI.Accounts.User.changeset/2
(kegcopr_api) lib/kegcopr_api/accounts/user.ex:35: KegCopRAPI.Accounts.User.registration_changeset/2
、私がユーザーを作成する方法上の任意のヘルプを以下のエラーを与えている述べました。現時点では、エラーがiexチェンジセットコマンドに入力されているか、またはuser.ex
ファイル内にあるかどうかはわかりません。
フィールドリストをアトムのリストとして送信してみましょう: '〜w(パスワード)' - ''〜w(パスワード)a'。 – Dogbert