I’m just building a basic todo app, and am unable to populate my list of todos. After defining a variable, which is initialized with my repo list, I’m inspecting the element. I’m able to see the empty list. I know the list exists, so I guess that’s step one.
Everytime I add an entry in my text field and click submit, the liveview reloads, but without populating the repo. What am I missing here?
@impl true
def mount(_params, _session, socket) do
socket = assign(socket, todo_list: Lists.list_lists())
IO.inspect(socket)
{:ok, socket}
end
@impl true
def render(assigns) do
~H"""
<form action="#" phx-submit="add">
<%= text_input :todos, :title, placeholder: "Add a todo" %>
<%= submit "Add", phx_disable_with: "Adding..." %>
</form>
<h1>Todos are here:</h1>
<%= for list <- @todo_list do %>
<p>list <%= list %></p>
<% end %>
"""
end
@impl true
def handle_event("add", %{"todo" => todo}, socket) do
Lists.create_list(todo)
{:ok, assign(socket, todos: Lists.list_lists())}
end
my render is a heex file. I had thought initially that I was mixing up heex and leex?
<form phx-submit="add">
<%= text_input :input_value, :title, placeholder: "Add a todo" %>
<%= submit "Add", phx_disable_with: "Adding..." %>
</form>
<h1>Todos are here:</h1>
<%= for list <- @todo_list do %>
<p>list <%= list %></p>
<% end %>
Right, as the error message says, it’s trying to give you the arg %{"input_value" => %{"title" => }} but your def handle_event expects the pattern %{"todo" => todo}. This causes an error.