Reputation: 181
I see lots of people posting about this same problem, but none of the answers have worked for me so far. Here's what I've got:
In application.html.erb
:
<%= link_to 'Logout', destroy_user_session_path, :method => :delete %>
Which produces the following HTML:
<a rel="nofollow" data-method="delete" href="/users/sign_out" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);">Logout</a>
Unfortunately this is linking to Users#Show instead of posting as a DELETE to Users/Sessions#Destroy as it is supposed to.
My config/routes.rb
has:
devise_for :users, controllers: { sessions: 'users/sessions', registrations: 'users/registrations' }
resources :users, only: [:show, :edit, :update]
And rake routes
returns:
new_user_session GET /users/sign_in(.:format) users/sessions#new
user_session POST /users/sign_in(.:format) users/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) users/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) users/registrations#cancel
new_user_registration GET /users/sign_up(.:format) users/registrations#new
edit_user_registration GET /users/edit(.:format) users/registrations#edit
user_registration PATCH /users(.:format) users/registrations#update
PUT /users(.:format) users/registrations#update
DELETE /users(.:format) users/registrations#destroy
POST /users(.:format) users/registrations#create
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
So the link_to
destroy_user_session_path
with :method => :delete
should work.
My application.js
has:
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require autocomplete-rails
//= require turbolinks
//= require skel.min
//= require_tree .
I also have the following in the <head>
of application.html.erb
:
<%= csrf_meta_tags %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
These are all the suggested solutions that I have seen, but none of them work. The only other solution I have seen enables a GET
method to logout, but I'd like to keep it RESTful if possible.
Any ideas?
Upvotes: 1
Views: 976
Reputation: 5626
For anyone running into this, check your developer console in the browser for any javascript errors. These delete requests are handled by a javascript controller and any errors will cause them to stop working.
Upvotes: 1
Reputation: 828
Remove resources :users, only: [:show, :edit, :update]
and try if log out works.
Then see this doc : How To: Define resource actions that require authentication using routes.rb
Hope it helps!
Upvotes: 0