To do this, we use another Open Source project named Ecto.
defmodule Discuss.Topic do use Discuss.Web, :model # required for model file schema "topics" do field :title, :string end # second requirement def changeset(struct, params \\ %{}) dp struct |> cast(params, [:title]) |> validate_required([:title]) end end
Validation in Phoenix is a little more challenging than it needs to be.
The changeset is the object that records the updates in our database that we need to make to take what it is right now (struct) to what it needs to be (params).
Note that params \\ %{}
Ecto is responsible with all the Repo
module that is responsible for all conversations with the database.
def create(conn, %{"topic" => topic}) do changeset = Topic.changeset(%Topic{}. topic) case Repo.insert(changeset) do {:ok, post} -> IO.inspect(post) {:error, changeset} -> IO.inspect(changeset) end end
Use the link for Ecto to get more information.
def index(conn, _params) do end