Cannot expand struct

When i try to start the server or run the tests i get this error:

== Compilation error on file web/controllers/user_controller.ex ==
** (CompileError) web/controllers/user_controller.ex:9: SchoolDiary.User.__struct__/1 is undefined, cannot expand struct SchoolDiary.User
    (stdlib) lists.erl:1354: :lists.mapfoldl/3
    (stdlib) erl_eval.erl:670: :erl_eval.do_apply/6

here is my controller:

defmodule SchoolDiary.UserController do
  use SchoolDiary.Web, :controller

  alias SchoolDiary.User

  plug :scrub_params, "user" when action in [:create]

  def create(conn, %{"user" => user_params}) do
    changeset = User.registration_changeset(%User{}, user_params)

    case Repo.insert(changeset) do
      {:ok, user} ->
        conn
        |> put_status(:created)
        |> render("show.json", user: user)
      {:error, changeset} ->
        conn
        |> put_status(:unprocessable_entity)
        |> render(SchoolDiary.ChangesetView, "error.json", changeset: changeset)
    end
  end
end

and my model:

defmodule SchoolDiary.User do
  use SchoolDiary.Web, :model

  schema "users" do
    field :email, :string
    field :password_hash, :string
    field :password, :string, virtual: true

    timestamps
  end

  def changeset(model, params \\ :empty) do
    model
    |> cast(params, ~w(email), [])
    |> validate_length(:email, min: 1, max: 255)
    |> validate_format(:email, ~r/@/)
  end

  def registration_changeset(model, params \\ :empty) do
    model
    |> changeset(params)
    |> cast(params, ~w(password), [])
    |> validate_length(:password, min: 6)
    |> put_password_hash
  end

  defp put_password_hash(changeset) do
    case changeset do
      %Ecto.Changeset{valid?: true, changes: %{password: pass}} ->
        put_change(changeset, :password_hash, Comeonin.Bcrypt.hashpwsalt(pass))
      _ ->
        changeset
    end
  end
end

What is your Elixir version? Do you have a sample application that reproduces the error?

1.3.2. I will upload a branch of my app an send you the link tonigth.

Here is the link of my app’s branch to reproduce the post title error:
https://bitbucket.org/cristiano_carvalho/school_diary/src/c4b52b47b42d415d1ca5c692458ba1f3770f0bcd/?at=user_auth

That’s a dead link

Sorry. I will fix it soon as possible.

https://cristiano_carvalho@bitbucket.org/cristiano_carvalho/school_diary_api.git

Folow the link above. The error is inside the branch auth_user.

I followed this tutorial from codeship:
https://blog.codeship.com/ridiculously-fast-api-authentication-with-phoenix/

Your file is named user.exs while it should have been user.ex. user.exs files are not compiled.

4 Likes

Thanks for reply. Now it worked!