# Problem with layout for partials

**URL:** <https://www.ruby-forum.com/t/problem-with-layout-for-partials/157595>\
**Category:** Rails\
**Created:** [January 13, 2009, 1:48pm UTC](https://www.ruby-forum.com/t/problem-with-layout-for-partials/157595 "2009-01-13T13:48:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![morgler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e7dd6/32.png) [@morgler](https://www.ruby-forum.com/u/morgler)\
**Post date:** [January 13, 2009, 1:48pm UTC](https://www.ruby-forum.com/t/problem-with-layout-for-partials/157595/1 "2009-01-13T13:48:59Z")

</div>

I like the feature to have layouts even for partials now. But I have a  
problem related to this topic:

My controller sets the variable @my\_profile somewhere before  
rendering. In the view I have the following code:

\<%= debug @my\_profile %\>  
\<% if me? %\>  
\<%= render :partial =\> “profiles/private”, :layout =\> ‘layout’ %\>  
\<% else %\>  
\<%= render :partial =\> “profiles/public”, :layout =\> ‘layout’ %\>  
\<% end %\>

The “debug” outputs the correct value. The two actual partials are not  
important, but the \_layout partial is where the trouble begins. In  
case this “if” renders the second option (the \_public partial), I can  
also trace the @my\_profile in the \_layout partial - everything is  
fine. However, if the above statement branches to the first option  
(\_private partial), tracing the @my\_profile variable in the \_layout  
partial gives me a DIFFERENT value (actually another valid profile,  
but not the one it is supposed to be)! I have no idea, how @my\_profile  
is set to a different value?! Is it possible for a controller action  
to happen in the middle of rendering a view? I can’t imagine that,  
since a partial has no action associate with it (and neither has a  
partial layout). Does anyone have an idea how this value could become  
set differently during the parital rendering process?

I would appreciate any kind of help! Thanks.

---

<div class="post-metadata">

**Author:** ![morgler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e7dd6/32.png) [@morgler](https://www.ruby-forum.com/u/morgler)\
**Post date:** [January 13, 2009, 2:10pm UTC](https://www.ruby-forum.com/t/problem-with-layout-for-partials/157595/2 "2009-01-13T14:10:48Z")

</div>

I tried the following workaround:

\<%= render :partial =\> “profiles/private”, :layout =\>  
‘layout’, :locals =\> {:my\_profile =\> @my\_profile } %\>

Now I can access the correct profile by using the local variable  
“my\_profile” in the \_layout partial. But the question remains: How  
come the @my\_profile has a different value in the above view code than  
in the \_layout partial? Is this a bug in rails?

---

<div class="post-metadata">

**Author:** ![morgler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e7dd6/32.png) [@morgler](https://www.ruby-forum.com/u/morgler)\
**Post date:** [January 13, 2009, 2:14pm UTC](https://www.ruby-forum.com/t/problem-with-layout-for-partials/157595/3 "2009-01-13T14:14:56Z")

</div>

maybe try the following:

restructure your code to have your debug-statement inside the if-  
clause (the first branch).  
post the code for the private partial.  
finally, tell us what @my\_profile is before the render-command and  
then inside the partial (at the beginning).  
it should really be the same.

---

<div class="post-metadata">

**Author:** ![morgler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e7dd6/32.png) [@morgler](https://www.ruby-forum.com/u/morgler)\
**Post date:** [January 13, 2009, 2:39pm UTC](https://www.ruby-forum.com/t/problem-with-layout-for-partials/157595/4 "2009-01-13T14:39:39Z")

</div>

Hey, thanks for the instructions. Here is the result:

Placing a “debug @my\_profile” anywhere ( before the “if” statement,  
inside the first branch of the “if” statement, inside the \_private  
partial) leads to the correct value (the first\_name of the sample  
profile is “De”). At the same time placing the debug command inside  
the \_layout partial gives me a different value (first\_name =  
‘Martha’):

show.html.erb:  
\<%= debug @my\_profile.first\_name %\> # =\> “De”  
\<% if params[:public\_view].blank? && @my\_profile && me? %\>  
\<%= debug @my\_profile.first\_name %\> # =\> “De”  
\<%= render :partial =\> “profiles/private”, :layout =\>  
‘layout’, :object =\> @my\_profile %\>  
\<% else %\>  
\<%= render :partial =\> “profiles/public”, :layout =\> ‘layout’ %\>  
\<% end %\>

\_private.html.erb:  
\<% top\_bar do %\>  
\<% @user = @profile.user %\>

# Über mich: \<%= " #{@profile.first\_name} [#{@user.login}]" %\> \<% end %\>

\<% left\_sidebar do %\>  
\<%= link\_to ’

  ![](https://www.ruby-forum.com/images/%0Auser_edit.png) Profil  
bearbeiten 
', edit\_profile\_path  
(@my\_profile), :class=\>‘button’ %\>  
\<%= link\_to ’
  ![](https://www.ruby-forum.com/images/%0Acamera_edit.png) Fotos  
bearbeiten 
', profile\_photos\_path  
(@my\_profile), :class=\>‘button’ %\>  
\<% end %\>

\<%= debug '\_private: ’ + @my\_profile.first\_name %\> # =\> “De”

\<%= render :partial =\> ‘profiles/bio’ %\>  
\<%= render :partial =\> ‘profiles/getting\_started’ %\>  
\<%= render :partial =\> ‘comments/list’, :locals =\> {:with\_form =\>  
true, :with\_wall\_to\_wall =\> true} %\>  
\<%= render :partial =\> ‘photos/gallery’, :locals =\> {:mini =\> true} %\>  
\<%= render :partial =\> ‘friends/list’, :locals =\> {:type =\> ‘friends’}  
%\>  
\<%= render :partial =\> ‘profiles/recent\_activity’ %\>

\_layout.html.erb:

\<%= debug '\_layout: ’ + @my\_profile.first\_name %\> # =\>  
“Martha” ???

 \<%= yield(:top\_bar) %\> 

\<%= render :partial =\> ‘shared/livesidebar’ %\>

```
<!-- LEFT_SIDEBAR -->
<div id="leftcontent">
  <div id="profilebox">
    <div class="roundedImage">
      <%= icon @profile, :big %>
    </div>
  </div>
  <%= yield(:left_sidebar) %>
</div>

<!-- MAIN -->
<div id="rightcontent">
  <%= yield %>
</div>

```

I just don’t get it - it seems weird.

---

<div class="post-metadata">

**Author:** ![morgler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e7dd6/32.png) [@morgler](https://www.ruby-forum.com/u/morgler)\
**Post date:** [January 13, 2009, 2:56pm UTC](https://www.ruby-forum.com/t/problem-with-layout-for-partials/157595/5 "2009-01-13T14:56:09Z")

</div>

\<%= render :partial =\> “profiles/private”, :layout =\>  
‘layout’, :object =\> @my\_profile %\>  
now that you’re passing :object to your partial you should be able to  
access @private which equals @my\_profile. does this lead to the same  
behaviour?

---

<div class="post-metadata">

**Author:** ![morgler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e7dd6/32.png) [@morgler](https://www.ruby-forum.com/u/morgler)\
**Post date:** [January 13, 2009, 3:47pm UTC](https://www.ruby-forum.com/t/problem-with-layout-for-partials/157595/6 "2009-01-13T15:47:22Z")

</div>

usually it should work without the “:object =\> @my\_profile” part (and  
i tried that). however, since relying on global variables like  
@my\_profile in a partial isn’t nice coding style anyway. so i tried to  
pass the variable explicitly to the partial. i would have expected the  
variable @my\_profile to appear as “layout” in my \_layout partial (and  
as “private” in my \_private partial). however, the “layout” variable  
is not defined in my \_layout partial :(. btw: i also tried “@layout”,  
but this isn’t defined either).

---

<div class="post-metadata">

**Author:** ![morgler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e7dd6/32.png) [@morgler](https://www.ruby-forum.com/u/morgler)\
**Post date:** [January 13, 2009, 2:57pm UTC](https://www.ruby-forum.com/t/problem-with-layout-for-partials/157595/7 "2009-01-13T14:57:19Z")

</div>

a no, forget what i just said. this should only work, if you have a  
Private-model.  
sorry, getting a bit tired.
