Hi there.
I’m starting out with some basic Phoenix tutorials and am a little stuck with routing. I’m trying to create a basic URL shortener which allows a user to submit a URLs through a basic form. Each submission in the session will be listed below the form.
Expected user flow:
- User is always at the “new.html” form.
- User can submit form which is routed to “create” in the controller.
- Link is saved to database with a short hash.
- User is redirected back to “new” form where a ul list is updated with all the items they have added.
- If user leaves history is not persisted.
Controller:
def new(conn, _params) do
changeset = Resource.change_link(%Link{})
render(conn, "new.html", changeset: changeset)
end
def create(conn, %{"link" => link_params}) do
case Resource.create_link(link_params) do
{:ok, link} ->
conn
|> put_flash(:info, "Link created successfully.")
|> redirect(to: Routes.link_path(conn, :new))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
I’m struggling to see how after creation I get each of the “link” creations displayed on the “new.html” form.
Thanks for any pointers.