Adapter: Ecto.Adapter.Postgres

Hi i am new to phoenix and elixir and i am trying to make a textbox that insert 1 data into database and have some error :

== Compilation error in file lib/zivot_web/controllers/topic_controller.ex ==
** (KeyError) key :otp_app not found in: []
    (elixir) lib/keyword.ex:389: Keyword.fetch!/2
    lib/ecto/repo/supervisor.ex:62: Ecto.Repo.Supervisor.compile_config/2
    lib/zivot_web/controllers/topic_controller.ex:3: (module)
    (stdlib) erl_eval.erl:680: :erl_eval.do_apply/6
    (elixir) lib/kernel/parallel_compiler.ex:208: anonymous fn/4 in Kernel.ParallelCompiler.sp

repo.ex:

defmodule Zivot.Repo do
  use Ecto.Repo,
    otp_app: :zivot,
    adapter: Ecto.Adapters.Postgres
end

tabela.ex

defmodule Zivot.Tabela do
  use Ecto.Schema
  import Ecto.Changeset


  schema "topics" do
    field :topic, :string

    timestamps()
  end

  @doc false
#  def changeset(struct, params) do
#    struct
#    |> cast(params, [:topic])
#    |> validate_required([:topic])
#  end
end

defmodule ZivotWeb.Tabela do
  use Ecto.Schema
  import Ecto.Changeset
  def changeset(struct, params) do
    struct
    |> cast(params, [:topic])
    |> validate_required([:topic])
  end
end

defmodule ZivotWeb.TopicController do
  use ZivotWeb, :controller
  use Ecto.Repo
  alias Zivot.Tabela

 def new(conn, params) do
    struct = %Tabela{}
    params = %{}
    changeset = ZivotWeb.Tabela.changeset(struct,params)
    render conn, "new.html", changeset: changeset
  end
#  def create(conn, %{"tabela" => topic_params}) do
  #  changeset = ZivotWeb.Tabela.changeset(%Tabela{}, topic_params)
  #  case Repo.insert(changeset) do
  #    {:ok, post} ->
        #conn
      #  |> put_flash(:info, "Topic created")
      #  |> redirect(to: Routes.topic_path(conn, :index))
  #    {:error, changeset} ->
  #      render conn, "new.html", changeset: changeset
  #  end
  #end
end

new.html

<%= form_for @changeset, Routes.topic_path(@conn, :create), fn f -> %>
<%= text_input f, :topic %>
<%= submit "Save Topic" %>
<% end %>

When ever you use Ecto.Repo, you must provide the :otp_app option.

1 Like

Some files are misplaced…

defmodule ZivotWeb.Tabela do
  use Ecto.Schema
  import Ecto.Changeset
  def changeset(struct, params) do
    struct
    |> cast(params, [:topic])
    |> validate_required([:topic])
  end
end

This file does not belongs to the Web part, it belongs to Zivot part.

defmodule Zivot.Tabela do
  use Ecto.Schema
  import Ecto.Changeset


  schema "topics" do
    field :topic, :string

    timestamps()
  end

  @doc false
#  def changeset(struct, params) do
#    struct
#    |> cast(params, [:topic])
#    |> validate_required([:topic])
#  end
end

This file has no changeset, or commented, why?

And there is no context.

Can You provide the Phoenix version and the way You created the table?

There are generators that might help You building the application if You are a newcomer :slight_smile:

For example…

$ mix phx.new zivot
$ cd zivot
$ mix ecto.create
$ mix help | grep gen
mix clean              # Deletes generated application files
mix ecto.gen.migration # Generates a new migration for the repo
mix ecto.gen.repo      # Generates a new repository
mix local.phx          # Updates the Phoenix project generator locally
mix phx.gen.cert       # Generates a self-signed certificate for HTTPS testing
mix phx.gen.channel    # Generates a Phoenix channel
mix phx.gen.context    # Generates a context with functions around an Ecto schema
mix phx.gen.embedded   # Generates an embedded Ecto schema file
mix phx.gen.html       # Generates controller, views, and context for an HTML resource
mix phx.gen.json       # Generates controller, views, and context for a JSON resource
mix phx.gen.presence   # Generates a Presence tracker
mix phx.gen.schema     # Generates an Ecto schema and migration file
mix phx.gen.secret     # Generates a secret
$ mix help phx.gen.html