Could not find user with id = sign_out

In my rails application, the exit link does not work.

I checked my .rb routes, which are listed below, and my application.html.erb looks like this.

Getting the next error.

ActiveRecord::RecordNotFound in UsersController#show Couldn't find User with id=sign_out Rails.root: /Users/patrickwalsh/rails_projects/ytutorial Application Trace | Framework Trace | Full Trace app/controllers/users_controller.rb:4:in `show' lib/disable_assets_logger.rb:11:in `call' 

My routes .rb

 Refectory::Application.routes.draw do devise_for :users, :controllers => { :registrations => "users" } devise_scope :user do get 'login', to: "devise/sessions#new", as: "login" get 'logout', to: "devise/sessions#destroy", as: "logout" get 'logout', to: "users/sessions#destroy", as: "logout" get 'signup', to: "users#new", as: "signup" match '/users/:id', :to => 'users#show', :as => :user end root :to => 'tutorials#index' devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy' get 'users/:id' => 'users#show' end resources :tutorials resources :comments, only: [:show, :create, :update, :destroy] resources :tutorials do member { post :vote } end if Rails.env == "development" match 'errors/404' => 'errors#error_404' match 'errors/500' => 'errors#error_500' end unless Rails.application.config.consider_all_requests_local match '*not_found', to: 'errors#error_404' end match 'tagged' => 'tutorials#tagged', :as => 'tagged' end 

and my application.html, which seems to be following the correct route from what I see.

Any help is much appreciated!

 <% if current_user.present? %> <li><%= link_to "Log out", destroy_user_session_path, (:method => "delete") %></li> <% else %> <li><%= link_to "Log in", new_user_session_path %></li> <li><%= link_to "Sign up", new_user_registration_path %></li> <% end %> 

My user controller as well as I suspect this is a problem, but not sure what the error is.

 class UsersController < Devise::RegistrationsController def show @user = User.find(params[:id]) @tutorials = @user.tutorials end end 
+8
ruby ruby-on-rails devise
source share
6 answers

You need to move:

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

above your devise_scope . Rails searches for routes from the top of the Routes file. The URL output is your users/:id URL, so it tries to display the show action with sign_out being the identifier.

UPDATE:

In fact, do you really need the last line in the devise_scope block?

0
source share

I had the same problem. My routes were in the correct order, the link_to method link_to used correctly, and Rails continued to run the users/:id route with :id => :sign_out . I realized that this is because I deleted jquery_ujs from my application.js file ...

jquery_ujs processes the data-method attribute in links (generated by link_to when using the method option), which is used to determine the correct route, as described here: https://thoughtbot.com/blog/a-tour-of-rails-jquery-ujs

Therefore, make sure the following is included in application.js :

 //= require jquery //= require jquery_ujs 
+10
source share

If you call / users / sign _out directly from the URL, this will not work because the routes:

 destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy 

id est, it uses the DELETE method. You need to change the initializer of your project to:

 config.sign_out_via = :get 

another option would be to create a small form and its button with the DELETE method.

+2
source share

None of these solutions worked for me. Also this only happens in development mode for me ... I fixed it by adding

  if params[:id] = "sign_out" sign_out current_user return end 

in a given user function. Not the nicest solution, but it works ...

+1
source share

It 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 
+1
source share

Since the other answers did not work, I found that you can change the base path for each development endpoint, as described here . So, I did this for the user devise_for on routes.rb :

 devise_for :users, path: 'devise' 

Then all my Device routes started with devise instead of users so that the conflict would not disappear.

0
source share

All Articles