5
votes

My Rails 4 application has an User and and Admin model.

I have implemented devise for the Admin model which was created via rails_admin gem. The problem occurs when I attempt to logout: the app redirects in an unexpected page and logs show that GET requests keep being made, instead of DELETE ones.

Following research I have ensured that the logout link has the delete method specified as below

  • <%= link_to "Admin logout", destroy_admin_session_path, :method => :delete %>
  • in devise.rb the sign out is specified via delete config.sign_out_via = :delete

    The routes are as follow

    Mvc::Application.routes.draw do
      resources :users
      devise_for :admins
      mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
    root "users#index"
    end
    

    I am perfectly able to login without issue, but when the logout request is rendered in the browser into /admins/sign_out the session is still active, and the page still displays the admin dashboard. Interestingly the flash message shows 'Model 'S' could not be found' and I am really puzzled by what the logs shows

    Started GET "/admins/sign_out" for 127.0.0.1 at 2014-12-03 23:10:50 +0000
    Processing by RailsAdmin::MainController#show as HTML
    Parameters: {"model_name"=>"s", "id"=>"sign_out"}
    [RailsAdmin] Could not load model S, assuming model is non existing. (uninitialized constant S)
    
    
    Gemfile
    source 'https://rubygems.org'
    
    gem 'rails', '4.0.3'
    gem 'sqlite3'
    gem 'bootstrap-sass', '~> 3.3.1'
    gem 'uglifier', '>= 1.3.0'
    gem 'jquery-rails'
    gem 'turbolinks'
    gem 'jbuilder', '~> 1.2'
    gem 'simple_form'
    gem 'bcrypt-ruby'
    gem 'devise'
    gem 'rails_admin'
    
    
    group :assets do
     gem 'sass-rails', '~> 4.0.0' 
     gem 'coffee-rails', '~> 4.0.0'
     gem 'uglifier', '>= 1.3.0'
    
    end
    
    group :doc do
    
     gem 'sdoc', require: false
    end
    
    group :development do
     gem 'better_errors', '~> 2.0.0'
    end
    

    What I would like to achieve is that when the logout is executed, the admin session is destroyed and the admin is redirected to the root_path. I note that I currently only have the application and user controller and no session controller. From what I understand the devise engine should bring session management out of the box. Any suggestions, please?

    1
    The :method => :delete functionality comes from jquery_ujs. Is that present in your project? You may need to add //= require jquery_ujs to your application.js file - Evan Marks
    RailsAdmin is hijacking your route from Devise, and then because it doesn't recognize /admins/sign_out it is interpreting it as /admins/:id, routing to the Show method, and trying to match a model with an id of "sign_out". You'll need to find a way to disentangle RailsAdmin and Devise to get the routes working the way you need them to. - Joseph
    Thanks Joseph that makes sense, I'll keep digging in this direction. - user2313058

    1 Answers

    -1
    votes

    It's seems that you aren't using the application layout file. So try to add the following snippet of code to the html file

    <head>
      <title>Training</title>
      <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
      <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
      <%= csrf_meta_tags %>
    </head>