Rails,为什么link_to始终使用方法[GET],即使我已经指定方法[post]或[delete]

发布于 2025-02-10 14:37:12 字数 552 浏览 2 评论 0原文

我已经看到其他一些人也问了这个问题,但是我发现没有人得到一个令人满意的答案。

为什么rails link_to始终使用方法:get,即使应用了其他一些方法,但是当您将link_to更改为button_to它开始工作吗?

以以下行:以下行: <%= link_to“登录”,destroy_user_session_path,方法:: delete,class:“ link”%> vs <%= button_to“登录”,destroy_user_session_path,方法:: delete,class,class:“ link”%>

所有轨道文档告诉我method应该工作。我什至从那里复制了代码示例并进行了编辑以适合我的代码,但它仍然不起作用。尽管Button_to是一个足够的解决方法,但我的一半链接是链接,而其他半按钮则伪装成链接是很烦人的。

我正在使用Rails 7。

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请爱~陌生人 2025-02-17 14:37:12

更新:进一步谷歌搜索,我发现了这一点: https://github.com/rails/rails/rails/rails/rails/rails/rails/issues/issues/issues/issues/issues/issues /44185 ,有一个对我有用的解决方案。

基本上,方法被弃用,而新方法是使用

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

。如果这不起作用,请尝试

rails importmap:install
rails turbo:install stimulus:install

先运行命令。

至少对我有用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文