views:

33

answers:

2

My App has two UI states: - Signed IN - Signed Out

I've been using the following to determine which app/view/layout to use based on if the user is signed in or out with Devise:

  # Devise, way of using a different Layout for all the devise/non-signed in Views
  layout :layout_by_resource
  def layout_by_resource
    if devise_controller?
      "application_unauthorized"
    else
      "application"
    end
  end

The problem is once your signed in it uses the wrong layout? ideas?

I only want to use "application_unauthorized" if it's devise & the user is not signed in.

+3  A: 

Personally I would check using if current_user? rather than devise_controller?

Ryan Bigg
Agreed. The test `devise_controller?` tests whether you are inside a devise controller, not even whether there is a current user signed in. Ryan's answer is correct imho.
nathanvda
+2  A: 

Actually you should use the user_signed_in? method to check if the user is signed in. I noticed that current_user? might return true even if the user is currently not signed in.

So your code would look something like this:

layout :layout_by_resource
def layout_by_resource
  if user_signed_in?
    "application"
  else
    "application_unauthorized"
  end
end
Wolfgang
nice man Thanks!
AnApprentice