# How to: programmatically choose a partial to render

**URL:** <https://www.ruby-forum.com/t/how-to-programmatically-choose-a-partial-to-render/91150>\
**Category:** Rails\
**Created:** [April 5, 2007, 5:28am UTC](https://www.ruby-forum.com/t/how-to-programmatically-choose-a-partial-to-render/91150 "2007-04-05T05:28:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![d6veteran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/94ad74/32.png) [@d6veteran](https://www.ruby-forum.com/u/d6veteran)\
**Post date:** [April 5, 2007, 5:28am UTC](https://www.ruby-forum.com/t/how-to-programmatically-choose-a-partial-to-render/91150/1 "2007-04-05T05:28:16Z")

</div>

I have a scenario where after updating a section on a page with a  
partial, I then need to update that same area with a new partial. This  
is easy, however, at this stage I need to display partial A if the user  
is logged in, and B if the user is not logged in.

How do I programmatically decide which partial to render?

---

<div class="post-metadata">

**Author:** ![d6veteran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/94ad74/32.png) [@d6veteran](https://www.ruby-forum.com/u/d6veteran)\
**Post date:** [April 5, 2007, 12:05pm UTC](https://www.ruby-forum.com/t/how-to-programmatically-choose-a-partial-to-render/91150/2 "2007-04-05T12:05:02Z")

</div>

On Apr 5, 2007, at 05:28 , Arch S. wrote:

> I have a scenario where after updating a section on a page with a  
> partial, I then need to update that same area with a new partial.  
> This  
> is easy, however, at this stage I need to display partial A if the  
> user  
> is logged in, and B if the user is not logged in.
> 
> How do I programmatically decide which partial to render?

render(:partial =\> (user.logged\_in? ? ‘foo’ : ‘bar’))

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

–  
Jakob S. - [http://mentalized.net](http://mentalized.net)

---

<div class="post-metadata">

**Author:** ![d6veteran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/94ad74/32.png) [@d6veteran](https://www.ruby-forum.com/u/d6veteran)\
**Post date:** [April 5, 2007, 7:35pm UTC](https://www.ruby-forum.com/t/how-to-programmatically-choose-a-partial-to-render/91150/3 "2007-04-05T19:35:30Z")

</div>

Jakob S. wrote:

> On Apr 5, 2007, at 05:28 , Arch S. wrote:
> 
> > I have a scenario where after updating a section on a page with a  
> > partial, I then need to update that same area with a new partial.  
> > This  
> > is easy, however, at this stage I need to display partial A if the  
> > user  
> > is logged in, and B if the user is not logged in.
> > 
> > How do I programmatically decide which partial to render?
> 
> render(:partial =\> (user.logged\_in? ? ‘foo’ : ‘bar’))
> 
> and there’s also some more readable but more verbose variants, I  
> imagine.
> 
> –  
> Jakob S. - [http://mentalized.net](http://mentalized.net)

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)

---

<div class="post-metadata">

**Author:** ![d6veteran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/94ad74/32.png) [@d6veteran](https://www.ruby-forum.com/u/d6veteran)\
**Post date:** [April 5, 2007, 8:56pm UTC](https://www.ruby-forum.com/t/how-to-programmatically-choose-a-partial-to-render/91150/4 "2007-04-05T20:56:28Z")

</div>

Arch S. 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](http://www.rubycentral.com/book/tut_classes.html) has more information.

–  
Jakob S. - [http://mentalized.net](http://mentalized.net)

---

<div class="post-metadata">

**Author:** ![d6veteran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/94ad74/32.png) [@d6veteran](https://www.ruby-forum.com/u/d6veteran)\
**Post date:** [April 5, 2007, 9:41pm UTC](https://www.ruby-forum.com/t/how-to-programmatically-choose-a-partial-to-render/91150/5 "2007-04-05T21:41:37Z")

</div>

Jakob S. wrote:

> Arch S. 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](http://www.rubycentral.com/book/tut_classes.html) has more information.
> 
> –  
> Jakob S. - [http://mentalized.net](http://mentalized.net)

Ya this question has opened a whole can of worms actually 😉

I did create a class method, but I made the mistake of checking the  
session, which the model does not have access to.

So I’m trying to rethink the entire problem.

What is the better route here:

Create a class method User.logged\_in? and pass the session, or create a  
controller action . . . but then I wonder, can I call a controller  
action from the template. Still trying to understand the framework.

---

<div class="post-metadata">

**Author:** ![d6veteran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/94ad74/32.png) [@d6veteran](https://www.ruby-forum.com/u/d6veteran)\
**Post date:** [April 6, 2007, 12:21am UTC](https://www.ruby-forum.com/t/how-to-programmatically-choose-a-partial-to-render/91150/6 "2007-04-06T00:21:17Z")

</div>

reed wrote:

> 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
> 
> On Apr 6, 7:41 am, Arch S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

I must have some confusion around how to access controller methods from  
a view.

I’m calling

4: \<% if logged\_in? -%\>

From an rhtml, but I get the error: ActionView::TemplateError (undefined  
method `logged\_in?’ for #\<#Class:0x48d2c60:0x48d2c38\>)

I have that method defined in my Application Controller.

---

<div class="post-metadata">

**Author:** ![d6veteran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/94ad74/32.png) [@d6veteran](https://www.ruby-forum.com/u/d6veteran)\
**Post date:** [April 5, 2007, 10:55pm UTC](https://www.ruby-forum.com/t/how-to-programmatically-choose-a-partial-to-render/91150/7 "2007-04-05T22:55:40Z")

</div>

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

On Apr 6, 7:41 am, Arch S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![d6veteran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/94ad74/32.png) [@d6veteran](https://www.ruby-forum.com/u/d6veteran)\
**Post date:** [April 7, 2007, 12:35pm UTC](https://www.ruby-forum.com/t/how-to-programmatically-choose-a-partial-to-render/91150/8 "2007-04-07T12:35:28Z")

</div>

On Apr 6, 2007, at 00:21 , Arch S. wrote:

> From an rhtml, but I get the error: ActionView::TemplateError  
> (undefined  
> method `logged\_in?’ for #\<#Class:0x48d2c60:0x48d2c38\>)
> 
> I have that method defined in my Application Controller.

You need to expose the controller method to your view by using  
helper\_method \<[http://api.rubyonrails.org/classes/ActionController/](http://api.rubyonrails.org/classes/ActionController/)  
Helpers/ClassMethods.html#M000165\>.

–  
Jakob S. - [http://mentalized.net](http://mentalized.net)
