为什么这个 link_to 使用 GET 而不是像我指定的那样使用 DELETE?
Why does this link_to use GET rather than DELETE like I specified?
我正在使用 devise
,这是我的 link:
<%= link_to "Logout", logout_path, method: :delete, class: "dropdown-item" %>
这是我点击 link:
时的日志输出
Started GET "/logout" for ::1 at 2020-04-16 21:24:58 -0500
ActionController::RoutingError (No route matches [GET] "/logout"):
这是它生成的HTML:
<a class="dropdown-item" rel="nofollow" data-method="delete" href="/logout">Logout</a>
这是我的相关部分 routes.rb
:
devise_scope :user do
delete "logout", to: "devise/sessions#destroy"
end
根据@max 对我上述问题的评论:
The reason its being sent as a GET request is that something is
preventing the event listener declared by Rails UJS from working. This
could be that your application.js is missing
require("@rails/ujs").start()
or a script error. Without that event
listener its just a plain old link and browsers handle clicks on links
by sending a GET request. For something as critical as a log out
button I would consider hardening your app by using button_to instead
which works even if javascript is turned off completely.
所以我将 link_to
更改为 button_to
,效果非常好。
我正在使用 devise
,这是我的 link:
<%= link_to "Logout", logout_path, method: :delete, class: "dropdown-item" %>
这是我点击 link:
时的日志输出Started GET "/logout" for ::1 at 2020-04-16 21:24:58 -0500
ActionController::RoutingError (No route matches [GET] "/logout"):
这是它生成的HTML:
<a class="dropdown-item" rel="nofollow" data-method="delete" href="/logout">Logout</a>
这是我的相关部分 routes.rb
:
devise_scope :user do
delete "logout", to: "devise/sessions#destroy"
end
根据@max 对我上述问题的评论:
The reason its being sent as a GET request is that something is preventing the event listener declared by Rails UJS from working. This could be that your application.js is missing
require("@rails/ujs").start()
or a script error. Without that event listener its just a plain old link and browsers handle clicks on links by sending a GET request. For something as critical as a log out button I would consider hardening your app by using button_to instead which works even if javascript is turned off completely.
所以我将 link_to
更改为 button_to
,效果非常好。