ruby-on-rails – Why is the "devise_controller?" helper? returns false for my code?

Question:

Why devise_controller? returns false for the following code:

class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
layout :resource
protected
def resource
  if devise_controller?
    #"admin"
    puts "admin"
  else
    #"application"
    puts "app"
  end
end
end

while in the browser line go to " http://127.0.0.1:3000/admin/review "

routes.rb file:

 Rails.application.routes.draw do
  get 'admin/show'
  get 'admin/upload'
  get 'admin/review'
  get 'welcome/index'
  get 'welcome/portfolio'
  get 'welcome/about'
  get 'welcome/contact'
  get 'welcome/blog'
  get 'welcome/review'
 end

while for the rest of the get 'admin/show' and get 'admin/upload' routes, the helper returns true ….

Update

In general, the problem was solved in this way. Added filter to ApplicationController

before_filter :my_filter, unless: :devise_controller?

 def my_filter
    if params['controller'] == 'admin'
      render layout: "admin"
    end
 end

But the question essentially remains – why does the same thing work with failures through layout ?

Answer:

devise_controller? only checks if your class is inherited from the DeviseController class

def devise_controller?
   is_a?(::DeviseController)
end

Your controller inherits from ActionController::Base , so the condition may not always be met. To check the fact of authentication, it is better to use the Devise helpers: user_signed_in? and current_user . If you need to change the layout regardless of whether the user is authenticated or not – just change it in render (as you actually did in Update).

Scroll to Top