Forum: Ruby on Rails instance variable has different value in layout vs. view!

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ryan C. (Guest)
on 2007-07-13 07:38
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:
<title> <%= @item.name %> </title>

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.
Ryan C. (Guest)
on 2007-07-13 07:54
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.
This topic is locked and can not be replied to.