Search code examples
ruby-on-railsblockhelper

Rails helper with block and parameter for block


I'm trying to implement something like this in Rails 2.3.10:

<% helper_method(collection) do |object| %>
   <p> <%= object %> </p>
<% end %>

The result of this helper would be something like:

<li> <p> Object 1 </p> </li>
<li> <p> Object 2 </p> </li>

Essentially the method would wrap each <p> with a <li>. I'm not sure if my approach is the best and any help would be greatly appreciated.

I read about capture(&block) and concat but I can't seem to be able to access the object parameter.

Thx.


Solution

  • In Rails 2.3.x

    def helper_method(collection, &block)
      concat(capture(collection, &block))
    end