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?
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?
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.
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’):
<%= 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?
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).