morteza

morteza

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 ?

Showing Posts 1 to 10

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.

OvermindDL1

OvermindDL1

Except you can set the delete method in the form, you don’t need javascript at all. What the javascript is used for is the ‘are you sure’ question of it, which you can leave out if you don’t need it and/or want to get rid of javascript (you can emulate it well enough via CSS and DOM nodes anyway as I’ve done at work).

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

As per the HTML5 standards, raw links only method is GET (never PUT/POST/DELETE/etc… though they can potentially cause a HEAD or OPTION request), and raw links should not make effectful changes into a system, only forms should cause effectful changes.

morteza

morteza OP

thank you for response,can you explain this ? i am new to the web programming specially in front end

OvermindDL1

OvermindDL1

The phoenix_html ‘delete’ button function actually just makes a form that does nothing when clicked, it instead delegates the functionality to javascript to cause a popup box to ask if you are sure you want to delete it and if the user clicks Ok then it sends the command. I really don’t like that pattern as then it won’t work when I’m in, for example, elinks (as I often am), so they just don’t work for me. Using a proper form with just CSS to fix it up and ask a question on first expansion is far more reliable and ‘safe’.

morteza

morteza OP

if possible send me example about this , i dont like to use or see magic codes in my program and the delete link which generated by phoenix confuses me
or show me a way to dont use the phoenix_html.js

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.

morteza

morteza OP

thank you bro :pray::pray:

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? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews