morteza

morteza

Delete Method does not work without Brunch

Hey
when i use --no-brunch ,delete method does not work in front end ,why phoenix force me to use brunch or some things like that ? if i dont want to use brunch or webpack and … what should i do for working delete method in front end ?

Marked As Solved

lpil

lpil

Creator of Gleam

The browser does not natively support delete requests so for them to work you will need to include the Phoenix HTML Javascript file (phoenix_html/priv/static/phoenix_html.js at main · phoenixframework/phoenix_html · GitHub)

In removing brunch you will have also removed the mechanism through which this file was added previously. Load it on the page through a method of your choice and delete link will work again.

Also Liked

OvermindDL1

OvermindDL1

This is my delete button that I use:

 @doc """
  This must not be inside of another form!
  """
  def delete_button(path), do: delete_button(path, :raised, [])
  def delete_button(path, btn_type) when is_atom(btn_type), do: delete_button(path, btn_type, [])
  def delete_button(path, opts) when is_list(opts), do: delete_button(path, :raised, opts)
  def delete_button(path, btn_type, opts) do
    {form, opts} = Keyword.pop(opts, :form, [])
    {form_class, form} = Keyword.pop(form, :class, "")
    form = Enum.map(form, fn {k, v} -> ~E{ <%= dasherize(k) %>="<%= v %>"} end)
    opts = opts ++ [do: "Delete"]
    opts = Keyword.put(opts, :class, "btn--red #{opts[:class]}")
    ~E"""
    <form action="<%= path %>" class="link <%= form_class %>" method="post"<%= form %>>
      <input name="_method" type="hidden" value="delete">
      <input name="_csrf_token" type="hidden" value="<%= get_csrf_token() %>">
      <%= button(:submit, btn_type, opts) %>
    </form>
    """
  end

And if I want an ‘are you sure’ kind of thing then I instead just put it inside a pure CSS collapsible element that I use here with the title of it as something like ‘Dangerous options’ or whatever and the delete button inside of it, so it first has to be expanded then the delete button can be clicked or so.

abitdodgy

abitdodgy

What if you are submitting the form via a link, though? For example, a link that triggers a delete request? I think then you would still need the JS.

OvermindDL1

OvermindDL1

Just for clarity, this is the important bit of it. It is a form with the action being the path to delete to, the method is ‘post’ (just because browsers don’t support non get/post methods very well), but then you override the method with the hidden input named _method with it’s value set to delete, and of course a CSRF token as all good forms should have, and the button to actually submit the delete form request. :slight_smile:

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New

Other popular topics Top

Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement