[LRUG] Funny gotcha with blocks in partials.

Andrew France andrew-lrug at avito.co.uk
Mon Jun 22 09:13:39 PDT 2009


Hi Tim,

Tim Cowlishaw wrote:
> I've come across a strange issue writing helpers that take a block 
> argument, that i was wondering if someone might be able to shed some 
> light on? Basically, what happens is this:
...
> However, if i pass it a block that contains ERB template strings, it 
> breaks - it just returns the result of each call to the block direct 
> to the template being rendered, as it's executed, rather than 
> returning it as the result of the call to array_to_list itself:
>
> <% array_to_list(@flash_games, :class => "flash_games_grid") do |game, 
> array| %>
>         <a href="<%= game_path(:id => game.id) %>"><img src="<%= 
> game.image_url(:grid) %>" alt="A Game" width="128" height="128" /></a>
> <% end %>
>
> I have a suspicion that this is something the rails capture() method 
> might be needed for, but i'm struggling to understand how, Anyone seen 
> anything similar in the past?

Yes you're right, you need to capture(&block). I think the best (and 
oldest) clean example is the block_to_partial method at Err the Blog:
http://errtheblog.com/posts/11-block-to-partial
Which is slightly broken for me, so Google's cache is: http://is.gd/19loI

Hope that helps,
Andrew




More information about the Chat mailing list