__struct__/1 undefined in controller

Dear all,

how to solved this code error

Compiling 2 files (.ex)
error: Train2Web.Bisnis.Merchant.struct/1 is undefined, cannot expand struct Train2Web.Bisnis.Merchant. Make sure the struct name is correct. If the struct name exists and is correct but it still cannot be found, you likely have cyclic module usage in your code

13 │ changeset = Bisnis.change_merchant(%Merchant{})
│ ^

└─ lib/train2_web/controllers/merchant_controller.ex:13:40: Train2Web.MerchantController.new/2

== Compilation error in file lib/train2_web/controllers/merchant_controller.ex ==

I have made 2 page

  1. merchant_controller.ex in folder controller
  2. merchant.ex in my context of bisnis folder

here my code in merchant_controller.ex

defmodule Train2Web.MerchantController do
  use Train2Web, :controller

  alias Train2Web.Bisnis
  alias Train2Web.Bisnis.Merchant

  def index(conn, _params) do
    merchants = Bisnis.list_merchants()
    render(conn, :index, merchants: merchants)
  end

  def new(conn, _params) do
    changeset = Bisnis.change_merchant(%Merchant{})
    render(conn, :new, changeset: changeset)
  end

  def create(conn, %{"merchant" => merchant_params}) do
    case Bisnis.create_merchant(merchant_params) do
      {:ok, merchant} ->
        conn
        |> put_flash(:info, "Merchant created successfully.")
        |> redirect(to: ~p"/merchants/#{merchant}")

      {:error, %Ecto.Changeset{} = changeset} ->
        render(conn, :new, changeset: changeset)
    end
  end

  def show(conn, %{"id" => id}) do
    merchant = Bisnis.get_merchant!(id)
    render(conn, :show, merchant: merchant)
  end

  def edit(conn, %{"id" => id}) do
    merchant = Bisnis.get_merchant!(id)
    changeset = Bisnis.change_merchant(merchant)
    render(conn, :edit, merchant: merchant, changeset: changeset)
  end

  def update(conn, %{"id" => id, "merchant" => merchant_params}) do
    merchant = Bisnis.get_merchant!(id)

    case Bisnis.update_merchant(merchant, merchant_params) do
      {:ok, merchant} ->
        conn
        |> put_flash(:info, "Merchant updated successfully.")
        |> redirect(to: ~p"/merchants/#{merchant}")

      {:error, %Ecto.Changeset{} = changeset} ->
        render(conn, :edit, merchant: merchant, changeset: changeset)
    end
  end

  def delete(conn, %{"id" => id}) do
    merchant = Bisnis.get_merchant!(id)
    {:ok, _merchant} = Bisnis.delete_merchant(merchant)

    conn
    |> put_flash(:info, "Merchant deleted successfully.")
    |> redirect(to: ~p"/merchants")
  end
end

And here my merchant.ex code:

defmodule Train2.Bisnis.Merchant do
  use Ecto.Schema
  import Ecto.Changeset

  schema "merchants" do
	field :mid, :string
	field :nama_merchant, :string
	field :category, :string
	field :alamat, :string
	field :rme, :string
	field :kota, :string

	timestamps(type: :utc_datetime)
  end

	@doc false
	def changeset(merchant, attrs) do
		merchant
		|> cast(attrs, [:mid, :nama_merchant, :category, :alamat, :rme, :kota])
		|> validate_required([:mid, :nama_merchant, :category, :alamat, :rme, :kota])
	end
end

the error says my merchant.struct/1 is undefined

You aliased Train2Web.Bisnis…. instead of Train2.Bisnis….

1 Like

Thankyou very much Sir, it’s solved nice

Btw i try follow the instruction guide form phoenix docs, but not use the mix phx.gen.html just type manually becaus to learn to flow.
the alias in product_controller.ex and product.ex in context of catalog doesn’t use additional text Web, does it a bug or ?

That‘s not a bug, but meant to enforce further the idea that your application should be independent from the web interface.

2 Likes