cameron g
cameron g

Reputation: 31

Phoenix undefined function cast error

I am going along with the Programmming Phoneix book and I am getting an error

rumbl master % → iex -S mix
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:8:8] [ds:8:8:10] 
[async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Compiling 9 files (.ex)

== Compilation error in file web/models/user.ex ==
** (CompileError) web/models/user.ex:6: undefined function cast/4
(stdlib) lists.erl:1338: :lists.foreach/2
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
(elixir) lib/kernel/parallel_compiler.ex:198: anonymous fn/4 in 
Kernel.ParallelCompiler.spawn_workers/6
rumbl master % →

This is the user.ex file it is talking about

defmodule Rumbl.User do
  defstruct [:id, :name, :username, :password]

  def changeset(model, params \\ :empty) do
    model
    |> cast(params, ~w(name username), [])
    |> validate_length(:username, min: 1, max: 20)
  end
end

Upvotes: 1

Views: 426

Answers (1)

Badu
Badu

Reputation: 1082

import Ecto.Changeset in your Rumbl.User module. cast/4 is a function in Ecto.Changeset module

defmodule Rumbl.User do
   #import here
   import Ecto.Changeset
   defstruct [:id, :name, :username, :password]

   def changeset(model, params \\ :empty) do
     model
     |> cast(params, ~w(name username), [])
     |> validate_length(:username, min: 1, max: 20)
   end
end

Upvotes: 1

Related Questions