Sign-out link isn\'t working in my rails application.
I have checked my routes.rb which is listed below and my application.html.erb looks to follow the right path.
You need to move:
devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy'
over your devise_scope
. Rails is looking for routes from top of Routes file. Your sign out url matches users/:id
, hence it is trying to render show action with sign_out
being an id.
UPDATE:
Actually, do you really need the last line in your devise_scope
block?
This worked for me
#form
<%= link_to(destroy_user_session_path, {:class => "nav-link", :method => :delete}) do %>
<span class="sidebar-normal"> Logout </span>
<% end %>
#routes
devise_scope :user do
get '/users/sign_out' => 'devise/sessions#destroy'
end
Since non of the other answers worked, I found that you could change the base path for every Devise endpoint as described here. So, what I did was to user devise_for
on routes.rb
:
devise_for :users,
path: 'devise'
Then, all my Devise routes started with devise
instead of users
so the conflict was gone.
None of this solutions worked for me. Also it happens just in development mode for me... I fixed it by adding
if params[:id] = "sign_out"
sign_out current_user
return
end
in the set user function. Not the prettiest solution but it works...
If you are calling /users/sign_out directly from the URL it won't work because the routes is:
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
id est, it uses the DELETE method. You need to change your devise initializer to:
config.sign_out_via = :get
other option would be to create a small form and its button with DELETE as method.
I had the same issue. My routes were in the correct order, the link_to
method was properly used, and Rails kept triggering the users/:id
route with :id => :sign_out
. I figured out that was because I removed jquery_ujs
from my application.js
file...
jquery_ujs
handles the data-method
attribute in the links (generated by link_to
when you use the method option), which is used to determine the correct route as explained here: https://thoughtbot.com/blog/a-tour-of-rails-jquery-ujs
So just make sure the you have the following included in your application.js
:
//= require jquery
//= require jquery_ujs