![](/img/trans.png)
[英]Why do I get No Route Error in Rails 3 even though I have defined a route?
[英]Why do I get Rails 7 Routing Error although route is defined?
我正在按照 Michael Hartl 的 Rails 教程构建一个小型演示应用程序。 我被困在注销中。 这是我的路线.rb:
Rails.application.routes.draw do
resources :users
get "/login", to: "sessions#new"
post "/login", to: "sessions#create"
delete "/logout", to: "sessions#destroy"
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
# Defines the root path route ("/")
root 'users#index'
end
这是相关的 controller 操作:
def destroy
log_out
redirect_to root_url, status: :see_other
end
这是定义 log_out 的 session 助手:
def log_out
reset_session
@current_user = nil
end
这是视图中的链接标签:
<%= link_to "Log out", logout_path, data: { 'turbo-method': :delete } %></span>
当我单击注销链接时,我收到此错误。 预期行为:注销用户,重定向到登录屏幕。
我究竟做错了什么?
我不知道是不是因为 Turbo,或者 Turbo 是否安装正确。 我已将 gem 'turbo-rails' 添加到 Gemfile 并在之后运行 bundle 没有任何效果。
对我来说,添加方法: :delete 与 data-turbo 一起工作。
<span>
<%= link_to "Log out", destroy_user_session_path,method: :delete, data: { 'turbo-method': :delete } %></span>
用传递方法检查
<%= link_to "注销", logout_path, 方法: :delete, data: { 'turbo-method': :delete } %>
或者
<%= link_to "注销", logout_path, 方法: :delete %>
您正在通过 GET 访问 DELETE 方法路由。 你跑了吗
rails turbo:install
您可能希望通过 GET 而不是 config/devise.rb 中的 Delete 来更改注销
# The default HTTP method used to sign out a resource. Default is :delete.
config.sign_out_via = :get # <= change this from :delete to :get and remove the `method:` in your `link_to` helper
以下是参考资料: https://github.com/hotwired/turbo-rails https://github.com/heartcombo/devise/issues/5439
您可以将link_to
更改为button_to
<%= button_to "Log out", logout_path, method: :delete %>
这将不那么神奇但更可靠,因为使用的不是链接而是表单(在正常生活中,点击链接是一个GET
请求)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.