[LRUG] Funny gotcha with blocks in partials.

Chris Mear chris at feedmechocolate.com
Mon Jun 22 09:15:06 PDT 2009


2009/6/22 Chris Mear <chrismear at gmail.com>

> On 22 Jun 2009, at 16:31, Tim Cowlishaw wrote:
>
>  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?
>>
>
> Yeah, I've done something like this before, by building up the output using
> the concat method, and using capture to get the rendered results of blocks
> defined in the ERB template.


Thinking about it a little more, you could check whether you have a block,
and if so use the concat method, otherwise just construct and return a
string. This would let you use both the:

  <%= array_to_list %>

and the:

  <% array_to_list do |e, a| %>
    stuff
  <% end %>

forms.

Chris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20090622/bcb22972/attachment.html>


More information about the Chat mailing list