2
votes

I'm having a terrible time with Ecto Changeset. I'm trying to simply create a new form with an "empty" Changeset. The controller is hit and the Session.changeset(%Session{}) line is invoked. It then hits Site.Session.changeset. In there, the cast call fails with the below error. This full stacktrace is below. Elixir 1.4, umbrella app.

The controller:

defmodule Site.SessionController do
  use Site.Web, :controller
  alias Site.Session

  def login_form(conn, _params) do
    changeset = Session.changeset(%Session{})
    conn |> render "login_form.html", changeset: changeset
  end
end

The module:

defmodule Site.Session do
  import Ecto.Changeset

  defstruct [:email, :password]

  @required_fields ~w(email password)
  @optional_fields ~w()

  @doc """
  Builds a changeset based on the `struct` and `params`.
  """
  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, @required_fields ++ @optional_fields)
  end
end

The error:

function Site.Session.__changeset__/0 is undefined or private. Did you mean one of:
      * changeset/1
      * changeset/2

The stacktrace:

Request: GET /login
** (exit) an exception was raised:
    ** (UndefinedFunctionError) function Site.Session.__changeset__/0 is undefined or private. Did you mean one of:

      * changeset/1
      * changeset/2

        (site) Site.Session.__changeset__()
        (ecto) lib/ecto/changeset.ex:422: Ecto.Changeset.do_cast/4
        (site) web/controllers/session_controller.ex:9: Site.SessionController.login_form/2
        (site) web/controllers/session_controller.ex:3: Site.SessionController.action/2
        (site) web/controllers/session_controller.ex:3: Site.SessionController.phoenix_controller_pipeline/2
        (site) lib/site/endpoint.ex:1: Site.Endpoint.instrument/4
        (site) lib/phoenix/router.ex:261: Site.Router.dispatch/2
        (site) web/router.ex:1: Site.Router.do_call/2
        (site) lib/site/endpoint.ex:1: Site.Endpoint.phoenix_pipeline/1
        (site) lib/plug/debugger.ex:123: Site.Endpoint."call (overridable 3)"/2
        (site) lib/site/endpoint.ex:1: Site.Endpoint.call/2
        (plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
        (cowboy) 
1
Probably because you haven't declared a "schema" for Site.Session. I don't think cast works without the struct having a schema defined.Dogbert
I could have sworn I tried that hours ago. /head++desk. Thanks :)Matt Darby

1 Answers

3
votes

Dogbert's comment above is correct. I was missing the schema. I was thinking it wasn't going to be persisted and I had a defstruct in the module, but alas: Don't forget the schema, kids.

defmodule Site.Session do
  use Ecto.Schema
  import Ecto.Changeset

  @required_fields ~w(email password)
  @optional_fields ~w()

  schema "sessions" do
    field :email, :string
    field :password, :string
  end

  @doc """
  Builds a changeset based on the `struct` and `params`.
  """
  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, @required_fields ++ @optional_fields)
  end
end