2
votes

I'm getting an error message from Rails that it could not find a user with id 'sign_out'. The error comes up when I try to log out of a user session. The application is looking at the GET route instead of the DELETE route that I would like it to.

I've looked through the other posts with similar errors and they suggest that the routes are not configured properly. But I can't seem to figure out what's going on with my application. I've tried several things:

  • Switching the order of the devise routes and the user resources
  • Calling out the delete method explicitly
  • Various combinations of method/destory paths for the link to logout

None of these options seem to work for me.

Here are my relevant files:

routes.rb

Rails.application.routes.draw do
  root to: 'welcome#index'
  devise_for :users
  resources :users, only: [:index,:show]

  resources :images, only: [:index, :show, :update]
  resources :challenges
end

I also tried nesting and calling out the path directly:

devise_for :users do
  get '/users/sign_out' => 'devise/sessions#destroy'
end
resources :users, only: [:index,:show]

my link to log out:

%li= link_to "Logout", destroy_user_session_path, :method => :delete

Running rake routes (relevant routes):

                  Prefix Verb   URI Pattern                       Controller#Action
    destroy_user_session DELETE /users/sign_out(.:format)         devise/sessions#destroy
                   users GET    /users(.:format)                  users#index

Using the "Log out" link gets this error:

Couldn't find User with 'id'=sign_out

and the parameters associated with the request are:

Request

Parameters:

{"id"=>"sign_out"}

And for whatever reason, the link still generates a GET request:

Started GET "/users/sign_out" for ::1 at 2015-12-25 03:35:11 -0600
Processing by UsersController#show as HTML
Parameters: {"id"=>"sign_out"}

Does anyone have any recommendations on what to change/try next? Let me know if you need more information from any other files.

1
Do you have any JavaScript errors on the page? Are you including jquery_ujs in your application.js file?sevenseacat
No javascript errors that I can see. My application.js file is empty.voltaaage
I just put in a bootstrap template today. But I was getting this error before that.voltaaage
If it's empty, that's where your problem lies. You need to require jquery_ujs, like the standard Rails template file does.sevenseacat
Ah, good catch. I did just add those lines back: //= require jquery //= require jquery_ujs It looks like it still doesn't work though. Same error. I tried restarting the server as well. :\voltaaage

1 Answers

1
votes
  1. My application.js file needed the defaulted comments:

    //= require jquery

    //= require jquery_ujs

  2. My layouts/application.haml file needed the CSRF meta tags:

    = csrf_meta_tags

Thanks to sevenseacat for pointing me in the right direction!