Home > Enterprise >  Rails, why does link_to always use method [GET] even though I have specified method [POST] or [DELET
Rails, why does link_to always use method [GET] even though I have specified method [POST] or [DELET

Time:06-28

I have seen a few other people also ask this question, but no one that I have found has been given a satisfactory answer.

Why is it that rails link_to always uses method :get, even though some other method has been applied, but the moment you change the link_to to button_to it starts working?

Take, for example, the following line: <%= link_to "Log out", destroy_user_session_path, method: :delete, class:"link"%> vs <%= button_to "Log out", destroy_user_session_path, method: :delete, class:"link"%>

All of rails documentation tell me that method: :post should work. I have even copied code examples from there and edited it to suit my code, and it still doesn't work. While button_to is an adequate workaround, it is annoying to have half of my links being links, and the other half buttons disguised as links.

I am using rails 7.

Thanks.

CodePudding user response:

UPDATE: Googling further, I found this: https://github.com/rails/rails/issues/44185, which had a solution that worked for me.

Basically, method is deprecated, and the new way is to use

data: { turbo_method: :delete, turbo_confirm: "Are you sure?" }

instead. If this doesn't work, try running the commands

rails importmap:install
rails turbo:install stimulus:install

first.

It worked for me, at least.

  • Related