delete button hell - so in my app on edit pages I ...
# cfwheels
r
delete button hell - so in my app on edit pages I have a update button to left and delete to the right. ButtonTo() in the same form as regular submit will just submit the edit form and you get an error about method delete not accepted. linkTo() doesn't work with delete method. Has anyone done what I'm trying to do? I'm trying to do this on a site without jquery. I mention this because all apps in the past I have an old rails javascript that allows me to submit a delete link as DELETE method. So I do have a fallback but wondering what people are doing lately.
a
A "Delete" operation from a form in and HTML page should still be doing a POST, not a DELETE. Sounds to me like your routing is incorrectly listening for a HTTP DELETE request, which is never gonna come from a
<form>
on a web page. That's not how these things are done. Have a route listening for a POST, and in the POST handler work out what the operation is and call another method according to whether the submitted button value was "Edit" or "Delete". (pretty much guess work as you didn't give us any code to make a more informed analysis)
Copy code
// MyController.cfc
component {
    public function actionSlug() {
        if (params.buttonName == "Edit") {
            return handleEdit(params)
        }
        if (params.buttonName == "Delete") {
            return handleDelete(params)
        }
        // however one handles a 400 response in CFWheels
    }

    private function handleEdit(params) {
        // etc
    }

    private function handleDelete(params) {
        // etc
    }
}
(NB: there's probably some sorta CFWheels-idiotmatic way of doing that; it's just pseudo-code)
(plus also you wanna validate at least for the presence of
params.buttonName
before any of that... all that jazz goes without saying)
p
@risto I’m away from the computer so can’t send you much in sample code but you may be able to do this with htmx.org. You can map the delete button to actually send a DELETE verb to the backend and have the backend process it. I have some examples of buttons calling the backend in the TodoMVC example I just posted a couple days ago. I’ll try to get you an example in the morning.
r
@Adam Cameron Hi Adam, thanks for the response. You are correct I don't ever want to use DELETE in a form. I guess what I didn't explain correctly is I want it to be an href. Basically your typical link with a bootstrap button class on it. I'm aware that a link is always going to be a GET but people have been able to click a link and have it submit the url as DELETE method using javascript. So in the past ruby on rails used a rails.js library that allowed you to just put method="delete" on a link and send it as DELETE method. That's what I have always used because I used jquery in my apps. Now I'm using vanilla javascript and could certainly write a function with fetch or something but I got spoiled just putting data-method="delete" on my delete links. cfwheels 2 and above create delete routes to only accept the DELETE method when using resources("users") for example.
@Adam Cameron Yes, I could manually create a .get route for delete but by default wheels creates the main routes for you and delete accepts only delete method. Just wondering if anyone was using any other libraries to do this.
@Peter Amiri Thank you. http://htmx.org/ does look like what I was looking for. More modern version of the ancient rails.js. I have a lot of controllers so it's just convenience and laziness.
This is all I used to do with all the CRUD pages for delete. Nothing else and a proper delete call using method="DELETE" for a cfwheels 2 route.
Copy code
#linkTo(text="Delete",data_method="delete",key=contact.id,class="btn btn-sm btn-alt-danger btn-circle float-right",encode="attributes",data_confirm="Are you sure you wish to delete #contact.firstname# #contact.lastname#?")#
a
Sorry when you said "I'm trying to do this on a site without jquery" I took that to be an imprecise way of saying "not wanting to use JS", implying you wanted to only use mark-up and standard mark-up behaviour. My bad.
In that case "an error about method delete not accepted" sounds like you don't have a route on the backend of things listening for delete requests.
Is the routing error coming from CFWheels? Your narrative style of explaining things - rather than code and error messages, and some more context - is not as helpful as perhaps it could be.
r
@Adam Cameron CFwheels created the delete route properly. When I clicked on the routes table I could see it there. It was simply as you noted, an href is never going to use method="delete" without javascript. Using a library like Peter listed above a will allow me to do that without writing additional javascript. Looking back at the thread above I certainly should have been more precise. Can I get a do over:)
a
hahahahaha
r
message has been deleted
@Adam Cameron BTW, here are a couple images of the routes table and error if your still interested. I knew what was causing the error and I just wanted to ask how people were handling my scenario these days insted of my 2013 rails.js
👍 1
p
@risto try something like this:
<button hx-delete="/deleteRoute"
hx-swap="innerHTML" hx-target="#another-div"> Delete </button> <div id=“another-div”></div>
Sorry, mobile client is a pain to type into. Anyway, from the route on the backend after you’ve taken care of the delete do a renderText(“I was deleted”) that way you can send a message back to the client.
You can mark this up with Bootstrap to make it look like a bootstrap alert when it comes back. If you need something more than just a simple text message to come back, you can create a partial and do a renderPartial(“partial name”).
r
@Peter Amiri That works perfect. Thanks again!
p
@risto I'm glad it helped.