Instance variable has different value in layout vs. view!


#1

Hi folks,

My controller sets an instance variable (@item) which somehow contains a
different value in the layout than it does in the view.

The controller (items_controller), has this action:

def show
@item = Item.find(params[:id])
@owner = User.find(@item.owner_id)
return_to_main unless @item.status == $ITEM_STATUS_ACTIVE
@other_items_from_owner = @owner.available_items_for_rent([@item])
end

When I visit /items/show/123, it renders show.rhtml with all the correct
info for item #123 inside the page content. Good so far.

However, in my layout, I have:

<%= @item.name %>

And when I visit /items/show/123, the browser title contains the name of
a different item! Now, if I rename the variable to something like
@item_foo (in all places in the controller/layout/view), then it shows
the CORRECT value in the title bar (and the page content). What could
possibly cause the variable’s contents to be different in the layout
than in the view? I’m not using any caching, by the way.


#2

Never mind, mystery solved. I was thinking that the layout was rendered
before the view, but obviously it’s the other way around. Turns out one
of the partials in my view was inadvertently reassigning @item to
something else.

Now back to your regularly scheduled day || night.