# Helper method with block--with list iteration?

**URL:** <https://www.ruby-forum.com/t/helper-method-with-block-with-list-iteration/148037>\
**Category:** Rails\
**Created:** [September 8, 2008, 3:41pm UTC](https://www.ruby-forum.com/t/helper-method-with-block-with-list-iteration/148037 "2008-09-08T15:41:50Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [September 8, 2008, 3:41pm UTC](https://www.ruby-forum.com/t/helper-method-with-block-with-list-iteration/148037/1 "2008-09-08T15:41:50Z")

</div>

I’d like to use the helper method with block technique, but I can’t seem  
to make it work the way I want with a list. I’m imagining something like  
this:

\<% decorate\_list\_with\_max(list, 5) do |item| %\>  
Here’s the item name: \<%= item.name %\>  
\<% end %\>

decorate\_list\_with\_max(list, max, &block) {  
output = “”  
index = 0;  
list.each |item|  
output \<\< “Before item”;  
output \<\< capture(item, &block)  
output \<\< “After item”;  
index = index + 1  
break if index \> max  
end  
}

I think you get the idea? My helper gets a list, iterates over it  
passing to the block in the view on each iteration (and passing an  
argument to the block), with full control of the logic of the iteration,  
including stopping it in the middle, etc.

It seems like there ought to be a way to do this. But I’m not succeeding  
in making it work. Anyone done something like this before?

Oh, Rails 1.8.x, not 2 yet.
