3
votes

I'm receiving the following error while I sign out my application. This error is only found after I add an import concept in my application.

My routes.rb file:

Rails.application.routes.draw do
  devise_for :users
  resources :userdetails do
    collection {post :import}
  end
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root to: "userdetails#index"

devise_scope :users do
 get 'sign_in', to: 'devise/sessions#new'
 get 'sign_out', to: 'devise/sessions#destroy' 
end
end

Controller file:

class UserdetailsController < ApplicationController
  # before_action :set_userdetail, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
  # GET /userdetails
  # GET /userdetails.json
def import
    Userdetail.import(params[:file])
    redirect_to root_url, notice: "Data imported"
  end

Routes:

      Prefix Verb   URI Pattern                     Controller#Action
    new_user_session GET    /users/sign_in(.:format)        devise/sessions#new
        user_session POST   /users/sign_in(.:format)        devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format)       devise/sessions#destroy

Here's my Error:

Unknown action Could not find devise mapping for path "/sign_out". This may happen for two reasons: 1) You forgot to wrap your route inside the scope block. For example: devise_scope :user do get "/some/route" => "some_devise_controller" end 2) You are testing a Devise controller bypassing the router. If so, you can explicitly tell Devise which mapping to use: @request.env["devise.mapping"] = Devise.mappings[:user]

2

2 Answers

1
votes

Try to replace get 'sign_out', to: 'devise/sessions#destroy' with delete 'sign_out', to: 'devise/sessions#destroy'

Rails.application.routes.draw do
  devise_for :users
  resources :userdetails do
    collection {post :import}
  end
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root to: "userdetails#index"

devise_scope :users do
 get 'sign_in', to: 'devise/sessions#new'
 delete 'sign_out', to: 'devise/sessions#destroy' 
end
end
0
votes

If you want to log out via GET method, then just replace following in your /config/initializers/devise.rb

config.sign_out_via = :delete

with

config.sign_out_via = :get

and it should work. There is no need to manually make any changes in the generated /sign_out link HTML markup.

For more details I would suggest you to go through the initializer file to know about the default values used.

Also you can refer this Devise wiki page How To: Change the default sign_in and sign_out routes wherein there is mentioned about this config option (quoted below for quick view)

Note that if you are making use of the :sign_out_via configuration option, then the signout action above may cause errors. You can duplicate the default behavior (which changes from delete to get based on :sign_out_via) by specifying: