I had a partial to display a list of ‘stories’, done in the traditional
rails-tutorial kind of way - eg, an ‘article’ has a list of ‘stories’,
so i have a partial called ‘story_list’ that has this:
for story in @article.stories
blah blah - some html and erb that works with ‘story’
end
and i call it from the article’s show page with
Story list
<%= render :partial => ‘story/story_list’ %>
so far so standard, right? That all works fine.
However, in another place in my app i want to display a different set of
stories (ones just done by the current user). To stay nice and DRY, i
want to use just one partial to display the stories, whichever bunch of
stories makes up the collection. So, i moved the start and end of the
for loop out of the partial: now, the view page that calls the partial
decides what makes up the collection, and the partial shows a single
item. So now i have, in the calling view,
Story list
for story in @article.stories
<%= render :partial => ‘story/story_list’ %>
end
and the partial is now simply
blah blah - some html and erb that works with ‘story’
The problem is this: now, the partial doesn’t know what ‘story’ is - ie
the local variable ‘story’ isn’t being passed through to the partial.
But i thought that the partial was effectively pasted into the calling
view page before being processed? In which case it shouldn’t matter?
I can get around this by passing through ‘@story’ instead of ‘story’,
but i’d like to understand the problem rather than work around it. Can
anyone explain? Is a partial call effectively a method call, so local
vars won’t work, or something?
sorry for the long winded post…
max