Hey guys
Sorry if this sounds dumb but I’m at a loss for how to get this to work.
I’m trying out Phoenix 1.7rc and, after generating a new project and adding a Todo
model using mix phx.gen
, I still don’t understand how to use <.link>
to delete an item.
If I run mix phx.routes
, here’s what I get:
GET / AppWeb.PageController :home
GET /items/toggle/:id AppWeb.ItemController :toggle
GET /items AppWeb.ItemController :index
GET /items/:id/edit AppWeb.ItemController :edit
GET /items/new AppWeb.ItemController :new
GET /items/:id AppWeb.ItemController :show
POST /items AppWeb.ItemController :create
PATCH /items/:id AppWeb.ItemController :update
PUT /items/:id AppWeb.ItemController :update
DELETE /items/:id AppWeb.ItemController :delete
I’m only interested in accessing the DELETE in the last line.
I have noticed that in the generated .heex
files (inside lib/app_web/controllers/item_html/index.html.heex
), they actually have the deleting working properly.
<.table id="items" rows={@items} row_click={&JS.navigate(~p"/items/#{&1}")}>
<:col :let={item} label="Text"><%= item.text %></:col>
<:col :let={item} label="Person"><%= item.person_id %></:col>
<:col :let={item} label="Status"><%= item.status %></:col>
<:action :let={item}>
<div class="sr-only">
<.link navigate={~p"/items/#{item}"}>Show</.link>
</div>
<.link navigate={~p"/items/#{item}/edit"}>Edit</.link>
</:action>
<!-- if I click this button, it works properly -->
<:action :let={item}>
<.link href={~p"/items/#{item}"} method="delete" data-confirm="Are you sure?">
Delete
</.link>
</:action>
</.table>
I’m trying to achieve this. behaviour However, when I do something similar like:
<%= for item <- @items do %>
<li data-id={item.id} class={complete(item)}>
<div class="view">
<label><%= item.text %></label>
<.link
class="destroy"
navigate={~p"/items/#{item}"}
method="delete"
>
</.link>
</div>
</li>
<% end %>
It simply doesn’t work. It seems to ignore the method='delete'
line and just makes a GET request to /items/1
, for example.
Why is this not working? Should method
have a different value?
Thank you very much for the attention!