# Calling partials from a layout - once nil, once not nil

**URL:** <https://www.ruby-forum.com/t/calling-partials-from-a-layout-once-nil-once-not-nil/176158>\
**Category:** Rails\
**Created:** [September 25, 2009, 1:45pm UTC](https://www.ruby-forum.com/t/calling-partials-from-a-layout-once-nil-once-not-nil/176158 "2009-09-25T13:45:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![benol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@benol](https://www.ruby-forum.com/u/benol)\
**Post date:** [September 25, 2009, 1:45pm UTC](https://www.ruby-forum.com/t/calling-partials-from-a-layout-once-nil-once-not-nil/176158/1 "2009-09-25T13:45:23Z")

</div>

Hi,

Rails 2.3.3 + restful\_authentication.

current\_user is a method from lib/authenticated\_system.rb through  
“include AuthenticatedSystem” in application\_controller.rb

-------- home/\_current\_user.pl.html.erb : --------  
\<% if current\_user %\>  
NOT NIL  
\<% else %\>  
NIL  
\<% end %\>

-------- home/\_user\_box.pl.html.erb : --------  
\<% if current\_user %\>  
NOT NIL  
\<% else %\>  
NIL  
\<% end %\>

-------- layouts/layout.html.erb : --------  
\<%= render :partial =\> ‘/home/current\_user’ -%\>  
\<%= render :partial =\> ‘/home/user\_box’ %\>

----- OUTPUT: -------  
NIL  
NOT NIL

---- LOGS: -------  
[INFO : 0925 13:43:58 #9238] Rendering template within layouts/layout  
[INFO : 0925 13:43:58 #9238] Rendering home/index  
[DEBUG: 0925 13:43:58 #9238] Rendered home/\_current\_user.pl (0.2ms)  
[DEBUG: 0925 13:43:58 #9238] User Columns (1.9ms) SHOW FIELDS FROM  
`users`  
[DEBUG: 0925 13:43:58 #9238] User Load (0.2ms) SELECT \* FROM `users`  
WHERE (`users`.`id` = 1) LIMIT 1  
[DEBUG: 0925 13:43:58 #9238] Rendered home/\_user\_box.pl (6.9ms)  
[INFO : 0925 13:43:58 #9238] Completed in 16ms (View: 8, DB: 2) | 200 OK  
[[http://localhost/](http://localhost/)]

Why is that so? Why one time it is nil and the other time it is not nil?

Any help appreciated.

Regards.

---

<div class="post-metadata">

**Author:** ![benol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@benol](https://www.ruby-forum.com/u/benol)\
**Post date:** [September 25, 2009, 2:50pm UTC](https://www.ruby-forum.com/t/calling-partials-from-a-layout-once-nil-once-not-nil/176158/2 "2009-09-25T14:50:42Z")

</div>

Michal B. wrote:

> -------- home/\_current\_user.pl.html.erb : --------  
> \<% if current\_user %\>  
> NOT NIL  
> \<% else %\>  
> NIL  
> \<% end %\>
> 
> -------- home/\_user\_box.pl.html.erb : --------  
> \<% if current\_user %\>  
> NOT NIL  
> \<% else %\>  
> NIL  
> \<% end %\>
> 
> -------- layouts/layout.html.erb : --------  
> \<%= render :partial =\> ‘/home/current\_user’ -%\>  
> \<%= render :partial =\> ‘/home/user\_box’ %\>

> Why is that so? Why one time it is nil and the other time it is not nil?
> 
> Any help appreciated.
> 
> Regards.

It has to do with the name of the partial, Rails does some magic by  
looking at the name of the partial and then making a local variable  
(with the same name as the partial) accessible to that partial.

i.e. a partial entitled “\_current\_user” will have current\_user as a  
local whereas a partial entitled “\_user\_box” will have user\_box as a  
local.

This was new functionality introduced with rails 2 I believe…

Don’t forget that the render() can have :object and :locals passed into  
it…

hth

ilan
