I got this very weird problem this evening when I was building something like “filter-posts-by-multiple-tags” feature.
In the view template:
<%
tags = Tag.all
selected_tags ||= nil
%>
<ul>
<% tags.each do |tag| %>
<% logger.info("selected_tags size:"+ selected_tags.size.inspect) %>
<li><%= link_or_span(tag, selected_tags) %></li>
<% end %>
</ul>
In the TagsHelper:
module TagsHelper
def link_or_span(tag, selected_tags)
selected_tags.delete(tag)
link_to tag.title, tag
end
end
It outputs (in the log):
selected_tags size:2
selected_tags size:1
selected_tags size:0
What I expect:
selected_tags size:2
selected_tags size:2
selected_tags size:2
I think var “selected_tags” in the view template should be untouched each time in the loop since it’s in the different scope, isn’t it?
The ENV:
- ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-linux]
- Rails 3.1.0.rc4
replace to
because I think your code will show all tag without selected_tag?it is right