MagazijnWeb.Category.__struct__/1 is undefined, cannot expand struct MagazijnWeb.Category

I don’t understand the error:

defmodule MagazijnWeb.CategoryController do

use MagazijnWeb, :controller

alias MagazijnWeb.Category

def new(conn,params) do

changeset = Category.changeset(%Category{}, %{})

render conn,"new.html"

end

def create(conn,_params) do

end

end

the model is simple. The project is MagazijnWeb and there’s the model direct under.

defmodule Category do

use Ecto.Schema

import Ecto.Changeset

schema "category" do

    field :category, :string

end

def changeset(category, params \\ %{}) do

    category

    |> cast(params,[:category])

    |> validate_required([:category])

end

end

Thanks

1 Like

Hello and welcome,

You have named struct the wrong way…

But You are using…

Please think about formatting your post, and try not use the error stack trace as title of the OP.

Hi there, welcome to the forum.

Your module name is defined as Category, not MagazijnWeb.Category.

If you generated it from the command line through one of the phx.gen.* tasks it would have been called Magazijn.Category, without the “Web” part, and put under lib/magazijn/ to make it clear it is part of the core logic, separated from the web functionality.

alias MagazijnWeb.Category is trying to find a module defined as defmodule MagazijnWeb.Category do

If you remove the alias your current code trying to call Category.changeset should work, but I suggest you read up on Phoenix directory structure

1 Like

Hi Jlodijk, welcome to the community.

Can you post the stacktrace or error message in the Topic/Post instead of making it the title.

You can format the code using using code blocks by wrapping in 3 backticks as below:

```
Insert Your code Here
```

Error message which was in title

lib/magazijn_web/controllers/category_controller.ex:7: 
MagazijnWeb.Category.__struct__/1 is undefined, cannot expand struct MagazijnWeb.Category. 
Make sure the struct name is correct. If the struct name exists and is correct but it still cannot be found, 
1 Like

Hey jlodijk,

Welcome to the forum. can you please show the error? The problem I found in your code yet is that you have defined the schema module in the wrong way. it should be like this;

defmodule Magazijn.Category do instead of defmodule Category do
OR
you can also create all of your models schema by creating a directory in Magazijn such as,
defmodule Magazijn.Models.Category do.

It’s a better or more readable way to do this.

Thanks for the answer, you where right.

1 Like