I am trying to set the layout to “private” when the user is logged in. It would be perfect if I could do something like:
layout 'private' if current_user
Except it gives this error:
undefined local variable or method `current_user' for ApplicationController:Class
I’ve also tried doing this in the application controller:
before_filter :pick_the_layout
def pick_the_layout
if current_user
render :layout => "private"
else
render :layout => "public"
end
end
No luck with that one, either, which makes sense. Can anyone think of a better way to do this?
You can do it like this:
Obviously, this depends on
current_userbeingnilorfalseif the user is not logged in.You can see documentation for the usage of
layouthere:http://api.rubyonrails.org/classes/AbstractController/Layouts/ClassMethods.html#method-i-layout
and here with more examples:
http://api.rubyonrails.org/classes/AbstractController/Layouts.html