没有路线匹配[GET]“/users/sign_out”我可以删除我的帐户但不能注销

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

这是我的错误:

没有路由匹配 [GET]“/users/sign_out”

这是所有路线:

                                  Prefix Verb   URI Pattern                                                                                       Controller#Action
                        new_user_session GET    /users/sign_in(.:format)                                                                          devise/sessions#new
                            user_session POST   /users/sign_in(.:format)                                                                          devise/sessions#create
                    destroy_user_session GET    /users/sign_out(.:format)                                                                         devise/sessions#destroy
                       new_user_password GET    /users/password/new(.:format)                                                                     devise/passwords#new
                      edit_user_password GET    /users/password/edit(.:format)                                                                    devise/passwords#edit
                           user_password PATCH  /users/password(.:format)                                                                         devise/passwords#update
                                         PUT    /users/password(.:format)                                                                         devise/passwords#update
                                         POST   /users/password(.:format)                                                                         devise/passwords#create
                cancel_user_registration GET    /users/cancel(.:format)                                                                           devise/registrations#cancel
                   new_user_registration GET    /users/sign_up(.:format)                                                                          devise/registrations#new
                  edit_user_registration GET    /users/edit(.:format)                                                                             devise/registrations#edit
                       user_registration PATCH  /users(.:format)                                                                                  devise/registrations#update
                                         PUT    /users(.:format)                                                                                  devise/registrations#update
                                         DELETE /users(.:format)                                                                                  devise/registrations#destroy
                                         POST   /users(.:format)                                                                                  devise/registrations#create
                                 friends GET    /friends(.:format)                                                                                friends#index
                                         POST   /friends(.:format)                                                                                friends#create
                              new_friend GET    /friends/new(.:format)                                                                            friends#new
                             edit_friend GET    /friends/:id/edit(.:format)                                                                       friends#edit
                                  friend GET    /friends/:id(.:format)                                                                            friends#show
                                         PATCH  /friends/:id(.:format)                                                                            friends#update
                                         PUT    /friends/:id(.:format)                                                                            friends#update
                                         DELETE /friends/:id(.:format)                                                                            friends#destroy
                                    root GET    /                                                                                                 home#index
                              home_about GET    /home/about(.:format)                                                                             home#about
                      rails_health_check GET    /up(.:format)                                                                                     rails/health#show
                      pwa_service_worker GET    /service-worker(.:format)                                                                         rails/pwa#service_worker
                            pwa_manifest GET    /manifest(.:format)                                                                               rails/pwa#manifest
        turbo_recede_historical_location GET    /recede_historical_location(.:format)                                                             turbo/native/navigation#recede
        turbo_resume_historical_location GET    /resume_historical_location(.:format)                                                             turbo/native/navigation#resume
       turbo_refresh_historical_location GET    /refresh_historical_location(.:format)                                                            turbo/native/navigation#refresh
           rails_postmark_inbound_emails POST   /rails/action_mailbox/postmark/inbound_emails(.:format)                                           action_mailbox/ingresses/postmark/inbound_emails#create
              rails_relay_inbound_emails POST   /rails/action_mailbox/relay/inbound_emails(.:format)                                              action_mailbox/ingresses/relay/inbound_emails#create
           rails_sendgrid_inbound_emails POST   /rails/action_mailbox/sendgrid/inbound_emails(.:format)                                           action_mailbox/ingresses/sendgrid/inbound_emails#create
     rails_mandrill_inbound_health_check GET    /rails/action_mailbox/mandrill/inbound_emails(.:format)                                           action_mailbox/ingresses/mandrill/inbound_emails#health_check
           rails_mandrill_inbound_emails POST   /rails/action_mailbox/mandrill/inbound_emails(.:format)                                           action_mailbox/ingresses/mandrill/inbound_emails#create
            rails_mailgun_inbound_emails POST   /rails/action_mailbox/mailgun/inbound_emails/mime(.:format)                                       action_mailbox/ingresses/mailgun/inbound_emails#create
          rails_conductor_inbound_emails GET    /rails/conductor/action_mailbox/inbound_emails(.:format)                                          rails/conductor/action_mailbox/inbound_emails#index
                                         POST   /rails/conductor/action_mailbox/inbound_emails(.:format)                                          rails/conductor/action_mailbox/inbound_emails#create
       new_rails_conductor_inbound_email GET    /rails/conductor/action_mailbox/inbound_emails/new(.:format)                                      rails/conductor/action_mailbox/inbound_emails#new
           rails_conductor_inbound_email GET    /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                                      rails/conductor/action_mailbox/inbound_emails#show
new_rails_conductor_inbound_email_source GET    /rails/conductor/action_mailbox/inbound_emails/sources/new(.:format)                              rails/conductor/action_mailbox/inbound_emails/sources#new
   rails_conductor_inbound_email_sources POST   /rails/conductor/action_mailbox/inbound_emails/sources(.:format)                                  rails/conductor/action_mailbox/inbound_emails/sources#create
   rails_conductor_inbound_email_reroute POST   /rails/conductor/action_mailbox/:inbound_email_id/reroute(.:format)                               rails/conductor/action_mailbox/reroutes#create
rails_conductor_inbound_email_incinerate POST   /rails/conductor/action_mailbox/:inbound_email_id/incinerate(.:format)                            rails/conductor/action_mailbox/incinerates#create
                      rails_service_blob GET    /rails/active_storage/blobs/redirect/:signed_id/*filename(.:format)                               active_storage/blobs/redirect#show
                rails_service_blob_proxy GET    /rails/active_storage/blobs/proxy/:signed_id/*filename(.:format)                                  active_storage/blobs/proxy#show
                                         GET    /rails/active_storage/blobs/:signed_id/*filename(.:format)                                        active_storage/blobs/redirect#show
               rails_blob_representation GET    /rails/active_storage/representations/redirect/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations/redirect#show
         rails_blob_representation_proxy GET    /rails/active_storage/representations/proxy/:signed_blob_id/:variation_key/*filename(.:format)    active_storage/representations/proxy#show
                                         GET    /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format)          active_storage/representations/redirect#show
                      rails_disk_service GET    /rails/active_storage/disk/:encoded_key/*filename(.:format)                                       active_storage/disk#show
               update_rails_disk_service PUT    /rails/active_storage/disk/:encoded_token(.:format)                                               active_storage/disk#update
                    rails_direct_uploads POST   /rails/active_storage/direct_uploads(.:format)                                                    active_storage/direct_uploads#create

这是我的标题

<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
    <%= link_to 'Friend App', root_path, class:"navbar-brand"%>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="navbarText">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item">
        <%= link_to "about us", home_about_path, class:"nav-link" %>
      </li>
      <li>
        <%= link_to "friends", friends_path, class:"nav-link" %>
      </li>
      <li>
        <%= link_to "addfriend", new_friend_path, class:"nav-link" %>
      </li>
      <li>
        <%= link_to "Sign Up", user_session_path, class:"nav-link" %>
      </li>
      <li>
        <%= link_to "Sign In", new_user_registration_path , class:"nav-link" %>
      </li>
      <li>
        <%= link_to "Log out", destroy_user_session_path, method: :delete, class: "nav-link" %>      
      </li>
      <li>
        <%= link_to "Profile", edit_user_registration_path, class:"nav-link" %>
      </li>
    </ul>
  </div>
</nav>

我的

Gemfile

source "https://rubygems.org"

gem "rails", "~> 7.2.1"
gem "sprockets-rails"
gem "sqlite3", ">= 1.4"
gem "puma", ">= 5.0"
gem "importmap-rails"
gem "turbo-rails"
gem "stimulus-rails"
gem "jbuilder"
gem "tzinfo-data", platforms: %i[ windows jruby ]
gem "bootsnap", require: false
gem 'devise', '~> 4.9', '>= 4.9.4'

group :development, :test do
  gem "debug", platforms: %i[ mri windows ], require: "debug/prelude"

  gem "brakeman", require: false

  gem "rubocop-rails-omakase", require: false
end

group :development do
  gem "web-console"
end

group :test do
  gem "capybara"
  gem "selenium-webdriver"
end

和我的application_controller

class ApplicationController < ActionController::Base
  # Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
  allow_browser versions: :modern
end

我创建了一个新文件,创建了一个文件注册.rb,在其中放入了一些有关注销的代码

class SessionsController < ApplicationController
  def destroy
    reset_session
    redirect_to root_path, notice: 'Logged out successfully.'
  end
end

并且没有任何改变,比如删除方法不在其中或类似的东西

ruby devise ruby-on-rails-7
1个回答
0
投票

虽然您可以在这里逃避并只让链接发送 GET 请求,但这实际上不是一个好的解决方案,因为 GET 请求应该是幂等的并且存储在浏览器历史记录中。

更好的选择是定义正确的路线:

delete '/users/sign_out', as: :destroy_user_session       

并使用

button_to

<%= button_to "Log out", destroy_user_session_path, method: :delete, class: "nav-link" %>      

button_to
创建一个实际的表单,即使没有 JavaScript 也可以工作,这是强化应用程序的关键功能以防止损坏的好方法。

如果您绝对需要使用链接,则应使用

data: { turbo_method: :delete }
而不是
method: :delete
,因为 Rails Unobtrusive JavaScript 驱动程序 (UJS) 在 Rails 7 中已作为默认值删除,并且 现在建议使用 Turbo

<%= link_to "Log out", destroy_user_session_path, data: { turbo_method: :delete }, class: "nav-link" %>

Turbo 提供了旧功能的替代品,但关键区别在于数据属性是“命名空间”的,因此它为具有

data-turbo-method
属性的链接添加了一个事件处理程序。

© www.soinside.com 2019 - 2024. All rights reserved.