简体   繁体   English

Rails,即使我指定了方法 [POST] 或 [DELETE],为什么 link_to 总是使用方法 [GET]

[英]Rails, why does link_to always use method [GET] even though I have specified method [POST] or [DELETE]

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?为什么 rails link_to总是使用method :get ,即使已经应用了一些其他方法,但是当您将link_to更改为button_to时它开始工作?

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"%>例如,下面这行: <%= 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.所有 rails 文档都告诉我method: :post应该可以工作。 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.虽然 button_to 是一个适当的解决方法,但让我的一半链接是链接,而另一半按钮伪装成链接是很烦人的。

I am using rails 7.我正在使用rails 7。

Thanks.谢谢。

UPDATE: Googling further, I found this: https://github.com/rails/rails/issues/44185 , which had a solution that worked for me.更新:进一步谷歌搜索,我发现了这个: https ://github.com/rails/rails/issues/44185,它有一个对我有用的解决方案。

Basically, method is deprecated, and the new way is to use基本上, method已被弃用,新的​​方法是使用

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.至少它对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM