I have the following haml code which generates a button:
= link_to 'Delete',
MODEL_path(MODEL),
class: 'data-type-button btn btn-negative',
data: { confirm: 'delete it now!!! ... ?' },
method: :delete
After pressing the delete button generated, and confirming via the popup that I do want to proceed with the deletion, I am brought to an error screen. This screen tells me that I have an invalid authenticity token. The same code where link_to
is replaced with button_to
, however, works as it should and the item is deleted.
I want this to be a link_to
and not a button_to
because of the way that the resulting HTML code lines up. How can I get the link_to
to work?
Confirm you have the csrf meta tag in your layout and in the HTML HEAD section.
= csrf_meta_tag
As it turns out, link_to
passes parameters via the contents inside of the parenthesis following the path. So in order to get the authenticity token to be passed as a parameter while also using a link_to
, you can use the following modification of that code
= link_to 'Delete',
MODEL_path(id: MODEL.id, authenticity_token: form_authenticity_token),
class: 'data-type-button btn btn-negative',
data: { confirm: 'Delete it now!!! ... ?' },
method: :delete
I actually knew the answer when I posted the question, but after spending a lot of time trying to figure this out and not finding the solution directly on google anywhere, I wanted to post my solution to help any other lost souls out there. I don't know if that is bad practice on this website or not, but if it helps someone then I'm okay with it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With