Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I put a delete link within an update form?

I have a remote form for updating a 'Point' model. It's a pretty ordinary form with a submit button.

However, I'd like to also include a 'remove' button beside the 'update' button.

Unfortunately, there is a problem with this. When I click on the 'update' button it ends up deleting the entry - the delete link seems to hijack the update form.

EDIT: I think I know why the update button is deleting. When I add the delete link to the form it adds this input:

<input name="_method" type="hidden" value="delete">

Regardless of whichever button I press this "_method" param is being picked up!

Now, I know that I can just position the remove button outwith the form element but in this case I'm not allowed to.

I guess the remove button could just be another update submit button, but with an extra :remove_this parameter.

However, something about that doesn't feel right. Any ideas?

like image 311
digitalWestie Avatar asked May 13 '11 11:05

digitalWestie


2 Answers

Another curious workaround that does not require javascript, is to put the delete-form outside the update-form but leave a <label for="theSubmitButtonInTheDeleteForm"> inside the update form. The update-form will continue to work as expected, but clicking the label will submit the delete-form. Then just style the label as a button.

like image 168
George Adamson Avatar answered Oct 25 '22 06:10

George Adamson


I would recommend using a link rather than a button and style it like a button:

link_to("Remove", resource_url, method: :delete, class: "delete_button")
like image 20
Coenwulf Avatar answered Oct 25 '22 06:10

Coenwulf