How to: programmatically choose a partial to render

render(:partial => (user.logged_in? ? 'foo' : 'bar'))

and there's also some more readable but more verbose variants, I imagine.

Arch Stanton wrote:

I have the action "logged_in?" defined in my User class, but when I call user.logged_in? I get an error:

ActionView::TemplateError (undefined method `logged_in?' for User:Class)

So the question really is "How to check if a user is logged in"? It looks like you're trying to call logged_in? on the User class. For that to work your logged_in? method needs to be a class method. Ie it has to be defined with def self.logged_in? or something to that effect.

http://www.rubycentral.com/book/tut_classes.html has more information.

I am specifying layout based on login, which is similar to what you are doing...

I have this method in my ApplicationController def logged_in_user   @logged_in_user ||= User.find session[:user] end

To check if someone is logged in I write "if logged_in_user" but you can add a method to ApplicationController like this...

def logged_in?   logged_in_user ? true : false end

You need to expose the controller method to your view by using helper_method <ActionController::Helpers::ClassMethods.