No route matches [GET] "/users/sign_out"
This is what I get on inspecting 'Log Out'
<a rel="nofollow" data-method="delete" href="/users/sign_out">Log out</a>
This is what I get after clicking 'Log Out' No route matches [GET] "/users/sign_out"
This is my User.rb ` class User < ActiveRecord::Base
devise :database_authenticatable, :trackable, :validatable
devise :omniauthable, :omniauth_providers => [:google_oauth2]
def self.from_omniauth(access_token)
data = access_token.info
user = User.where(:email => data["email"]).first
unless user
user = User.create(
email: data["email"],
password: Devise.friendly_token[0,20]
)
end
user
end
end`
This is the output of rake routes Prefix Verb URI Pattern Controller#Action unauthorised GET /unauthorised(.:format) dashing/extras#unauthorised access GET /access(.:format) dashing/extras#access root GET / dashing/extras#home GET /:name(.:format) dashing/extras#show new_user_session GET /users/sign_in(.:format) devise/sessions#new user_session POST /users/sign_in(.:format) devise/sessions#create destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy user_google_oauth2_omniauth_authorize GET|POST /users/auth/google_oauth2(.:format) users/omniauth_callbacks#passthru user_google_oauth2_omniauth_callback GET|POST /users/auth/google_oauth2/callback(.:format) users/omniauth_callbacks#google_oauth2 dashing /dashing Dashing::Engine
Routes for Dashing::Engine: events GET /events(.:format) dashing/events#index GET /dashboards/:name(.:format) dashing/dashboards#show dashboards GET /dashboards(.:format) dashing/dashboards#index GET /widgets/:name(.:format) dashing/widgets#show PUT /widgets/:name(.:format) dashing/widgets#update root GET / dashing/dashboards#index
This is what my homepage says:
<h1> HOME </h1>
<%= link_to 'Sign In with Google', omniauth_authorize_path(:user, :google_oauth2) %>
<%= link_to "Log out", destroy_session_path(:user), method: :delete %>
Please use the mailing list or StackOverflow for questions/help, where a wider community will be able to help you. We reserve the issues tracker for issues only.
Facing the same issue, searched on stackoverflow. Over there people are telling to add external input.
For example:
devise_for :users do
get '/users/sign_out' => 'devise/sessions#destroy'
end
I solved this issue using jquery-ujs. If you look into the readme it says:
make non-GET requests from hyperlinks
@jorgearimitsu Thanks for the reminder on that one - I'd switched to webpack and hadn't imported jquery-ujs in my application pack, though I had remembered to add it to my yarn package:
import 'jquery-ujs';
Easy fix but could've been a headache if not for your comment. : )
config.sign_out_via = :get
For gem 'jquery-rails', '~> 4.3', '>= 4.3.5'
//= require jquery_ujs
config/initializers/devise.rb ->
# The default HTTP method used to sign out a resource. The Default is :delete.
# Changing to :get request solved my problem. Also, do remember to restart
# your server, else the changes will not be taken by the application.
config.sign_out_via = :get
If you want to use :delete method for security reasons and not be dependent on jquery-ujs you can use button_to instead of link_to, like:
button_to "Log out", destroy_user_session_path, method: :delete
If you want to use
:deletemethod for security reasons and not be dependent onjquery-ujsyou can usebutton_toinstead oflink_to, like:button_to "Log out", destroy_user_session_path, method: :delete
This is super useful since if you want to use https://github.com/hotwired/turbo-rails you may have removed ujs.
Please use the mailing list or StackOverflow for questions/help, where a wider community will be able to help you. We reserve the issues tracker for issues only.
@rafaelfranca How is the not an issue? He's using the API as specified, and it's not working like it's supposed to. Are you sure this isn't a bug in Rails?
@Andrew-CC-Martin the issue doesn't seem to be Devise or Rails, it appears he's set up a link with data-method=delete but is getting a GET request on the server, which most likely indicates that UJS is not present to handle the data-method for links. Which means it may be more of a config issue, so help required, not necessarily a bug/issue.
All of the suggestions above may apply to get it working:
- use
button_toinstead oflink_towithdata-method - if using links, use
rails-ujsso that it can apply differentdata-methods. - change
config.sign_out_via = :getas a last resort, if you're okay with logout via GET.
Are there any reasons why you would NOT be ok with signout via GET?
Are there any reasons why you would NOT be ok with signout via GET?
If I remember correctly, GET is expected to be a safe transaction that won't tell the server to do anything. POST on the other hand expect things to happen. Example; If your browser is helpfully speeding up your web interactions by preloading things you've not yet decided to read (and follows every link on a page to preload it), it will follow a GET request and in this case would unexpectedly log you out.
If you want to use
:deletemethod for security reasons and not be dependent onjquery-ujsyou can usebutton_toinstead oflink_to, like:button_to "Log out", destroy_user_session_path, method: :delete
Thank you so much, this solved my problem
If you want to use
:deletemethod for security reasons and not be dependent onjquery-ujsyou can usebutton_toinstead oflink_to, like:button_to "Log out", destroy_user_session_path, method: :delete
in result, this is the safest way, thank you
Use this with turbo link_to 'LogOut', destroy_user_session_path, data: { turbo_method: :delete }
config/initializers/devise.rb ->
# The default HTTP method used to sign out a resource. The Default is :delete. # Changing to :get request solved my problem. Also, do remember to restart # your server, else the changes will not be taken by the application. config.sign_out_via = :get
thanks it worked for me just don t forget to restart the server