Couldn't find User with id=sign_out
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?
Rails/Devise: Couldn't find User with 'id'=sign_out
My application.js file needed the defaulted comments:
//= require jquery
//= require jquery_ujs
My layouts/application.haml file needed the CSRF meta tags:
= csrf_meta_tags
Thanks to sevenseacat for pointing me in the right direction!
Error trying to logout: Couldn't find User with 'id'=sign_out
move get '/users/sign_out' => 'devise/sessions#destroy'
above resources :users
in your routes. Routes are given priority in terms of their order in the routes file.
On destroying session via Devise Couldn't find User with 'id'=sign_out
resources :users
and devise_for :users
are conflict so
try below code:
routes.rb
Rails.application.routes.draw do
root to: 'visitors#index'
devise_for :users
#resources :users //comment the users resources because its already used by devise_for or used some other resources for users
get "home/index"
get "home/minor"
root to: 'home#index'
end
Additionally, in your config/initalizer/devise.rb
file
Change config.sign_out_via = :delete
to config.sign_out_via = :get
Couldn't find User with 'id'=
Your controller is looking for "id" prior to hitting your action code, via..
before_action :set_user, only: [:show, :edit, :update, :destroy]
..which in turn is calling the "set_user" method and attempts...
User.find(params[:id])
However, your route defines the parameter as ":user_id", not ":id"...
get 'profile/:user_id' => 'users#show', as: :profile
Since you're writing your own .find query in your action, try removing ":show" from your before_action. Or alternatively you could DRY up a bit and just use ":id" everywhere to avoid confusion. :)
Couldn't find User with 'id'=9
You should try below,
def current_user
@current_user ||=User.find_by_id(session[:user_id]) if session[:user_id]
end
find_by_id
is finder method which will return nil if record for an id provided through session is not present.
Further you can add filter to redirect to login page by expiring session as session[:user_id] = nil
I had following case in Rails API app,
User.find_by_id(session[:user_id]) || redirect_to(new_user_session_url)
Couldn't find User with 'id'=admin
You should move the following line in your routes.rb
.
get '/users/admin', to: 'users#admin'
above
resources :users
The problem you got is that Rails routing system recognizes this path - /users/admin
as matching to /users/:id
and therefore routes to users#show
action.
ActiveRecord::RecordNotFound in HomeController#index Couldn't find User with 'id'=
current_user
will work only if user logged in. After logout it clears out session[:user_id]. That's why you can not find a user.
User.find(nil)
Before you call current user method that queries DB try to check that session[:user_id] present. That will work:
if(logged_in? && current_user && current_user.role == 'admin')
or
def current_user
return unless logged_id?
if(@current_user.present?)
return @current_user
end
@current_user = User.find(session[:user_id])
end
I think the best option here is to wrap render in if(logged_in?)
statement:
<div class="collapse navbar-collapse d-flex justify-content-end">
<% if(logged_in?) %>
<% if(current_user && current_user.role == 'admin') %>
<%= render partial: 'admin_user_header' %>
<% else %>
<%= render partial: 'registered_user_header' %>
<% end %>
<% else %>
<%= render partial: 'anonymous_user_header' %>
<% end %>
</div>
Related Topics
How to Redefine Fixnum's + (Plus) Method in Ruby and Keep Original + Functionality
How to Summarize Array of Integers as an Array of Ranges
Why am I Getting This Passenger Error Could Not Find Rake-0.9.2.2 in Any of the Sources
How to Write an Rspec Test for a Ruby Method That Contains "Gets.Chomp"
How to Run Ruby on Rails Applications on a Windows Box
How to Get a Linux Command Output to Chef Attribute
Splitting Routes File into Multiple Files
Regular Expression "Empty Range in Char Class Error"
Can't Reindex Sunspot Solr - Error - Rsolr::Error::Http - 500 Internal Server Error
Integrating @Font-Face Files into Rails Asset Pipeline
Ruby on Rails - Link_To Button/Css
How to Create Email with CSS and Images from Rails
Using SASS with User-Specified Colors
Time Gt Query Not Working with Mongoid and Ruby on Rails
Generate Nested Hashes from Strings and Deep Merging in Ruby