Search code examples
ruby-on-railsbuttonhyperlinkhamldestroy

Rails link_to destroy method fails due to invalid authenticity token


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?


Solution

  • Confirm you have the csrf meta tag in your layout and in the HTML HEAD section.

    = csrf_meta_tag