Rails link_to :method => :put 发送 GET 请求

问题描述 投票:0回答:3

我尝试使用 link_to 发送 PUT 请求,但每次我发送 GET 请求。

我试过了:

<%= link_to raw('Valider'), roommates_join_path(:reference => var['reference']), :method => :put, :class => "btn btn-primary" %>

在我的浏览器中我得到:

<a class="btn btn-primary" data-method="put" href="/roommates/join" rel="nofollow">Valider</a>

我的路线.rb:

 put 'roommates/join'

当我点击:

没有路线匹配 [GET]“/roommates/join”

ruby-on-rails httprequest put
3个回答
1
投票

我在使用

link_to path, method: :put, data: {}
时也遇到同样的问题,它会触发 AJAX 到
GET
PUT

link_to
更改为
button_to
以使用实际形式而不是使用
jquery_ujs
可以解决问题。不过,我并没有真正找到原因。


0
投票

这段代码是

device gem
的button_to、link_to代码。 如果你使用rails7,除了get方法之外,它将无法工作put,delete等。

这就是为什么不能使用 put 方法

link_to
https://discuss.rubyonrails.org/t/link-to-rails-7/79024

data: {"turbo-method": :delete}
中添加
link_to

按钮_至

<%= button_to "logout", destroy_user_session_path, method: :delete  %>

链接到

<%= link_to "Log out", destroy_user_session_path, data: {"turbo-method": :delete} %>

-2
投票

确保在布局中包含应用程序的 javascript。

例如,如果您使用默认的“应用程序”布局,请确保在

<head/>
中包含应用程序 javascript,如下所示:

应用程序/视图/布局/application.html.erb

<head> 
  ... 
  <%= javascript_include_tag "application" %>
  ...
</head>
© www.soinside.com 2019 - 2024. All rights reserved.