No route matches "/users/sign_out" devise rails 3

Viewed 135696

I've installed devise on my app and applied the following in my application.html.erb file:

<div id="user_nav">
    <% if user_signed_in? %>
        Signed in as <%= current_user.email %>. This cannot be cheese?
        <%= link_to 'Sign out', destroy_user_session_path %>
    <% else %>
        <%= link_to 'Register', new_user_registration_path %> or <%= link_to 'Sign in', new_user_session_path %>
    <% end %>
</div>

I ran rake routes and confirmed that all the routes are valid.

Also, in my routes.rb file I have devise_for :users and root :to => "home#index".

I get the following routing error when clicking the "Sign out" link:

No route matches "/users/sign_out"

Any ideas what's causing the error?

31 Answers

Use it in your routes.rb file:

devise_for :users do
    get '/users/sign_out' => 'devise/sessions#destroy'
end

I am using rails 7. So This was how I had to do it. The important bit is data: { turbo_method: :delete }

<%= link_to t('nav.logout'), destroy_user_session_path, class: "nav-link", data: { turbo_method: :delete } %>

Below were the defaults created by rails when I generated the project.

application.html.erb

<%= javascript_include_tag "application", "data-turbo-track": "reload", defer: true %>

application.js

import "@hotwired/turbo-rails"
import "./controllers"
import * as bootstrap from "bootstrap"

For Rails 7

We importmap the jquery and jquery-ujs manually in the importmap.rb :

pin "jquery", to: "https://ga.jspm.io/npm:jquery@3.6.0/dist/jquery.js"
pin "jquery-ujs", to: "https://cdnjs.cloudflare.com/ajax/libs/jquery-ujs/1.2.3/rails.min.js"

or for jquery use the:

./bin/importmap pin jquery

and enter manually the 2nd line for jquery-ujs

We create the file: app/javascript/src/jquery.js with the code:

import jquery from 'jquery'
window.jQuery = jquery
window.$ = jquery

Then we import them inside the app/javascript/application.js :

import "src/jquery"
import "jquery-ujs"

and we're done without having to change the config.sign_out_via = :delete of devise.rb

** Notice: For jquery it may require a yarn add jquery if for some reason the above doesn't work

Related