Simple String Concatenation of a Collection Written as a Helper for Rails

At Railsconf last week I took Greg Pollack’s online course Rails Best Practices. The interface is gorgeous and the instructions are excellent. One of the lessons involved taking a partial and moving it into a helper. I was reminded how difficult such a simple task can be. I have written about this before Yield a Block Within Rails Helper Method with Multiple Content Tags. However, that post aims to solve a slightly different problem, where the helper method takes the captured text from a block passed as an argument which essentially acts as a wrapper.

The difficulty isn’t with the logic itself and or the complexity/verbosity that the code is likely to produce. Rather, it is difficult because you have to endlessly concatenate strings and this something somewhat uncommon when programming with ruby. We have to remember that we’re working with a buffer of text about to be flushed. Here is a simple code snippet that shows how to write a helper method that loops over a collection of objects.

  def bookmarks_for(user=nil)
    content_tag(:div, :id => "bookmarks") do
      user.bookmarks.each do |bookmark|
        concat(
          content_tag(:strong) { bookmark.member_name } +
          excerpt(bookmark.body, '', 100)
        )
      end.join
    end
  end

Notice the use of the concat() method the “+” sugb and .join() method. All three of which bring these statements together into one final piece of html called in the view.

<%= bookmarks_for(@user) %>

Yield a Block within Rails Helper Method with Multiple Content_tags Using Concat

To clean up some repetitive html coding in views, pass a block of text to a helper function which will wrap it for you the same way, each and every time. For example, I have a ‘help’ link which will toggle the display of a block of text if a link is clicked. I use this “+/- help” link throughout my application in various views. I could create a partial but this gets messy. Instead this is what I want to write in my views…

&lt;% help do %&gt;
  here is my help text...
&lt;% end %&gt;

which will render HTML similar to this…

&lt;a href='#' 'class='help' id='help_link_123456' onclick='some_func_to_toggle_state'&gt;+/- help&lt;/a&gt;
&lt;div id="help_123456"&gt;
  here is my help text...
&lt;/div&gt;

In order to accomplish this you need to create a helper method. Assigning a unique id, just a random number, will help avoid collisions with the state of the toggled div if you use this more than once per page. Placing other HTML helper methods inside the concat() method allows multiple tags as well as rendering the block passed to the function in the appropriate place. It looks a little unwieldy, but works nicely.

# app/helpers/application_helper.rb
module ApplicationHelper
  def help(&block)
    uniqid = rand; concat( link_to_function("+/- help") do |page|
      page["help_#{uniqid}"].toggle
      page.visual_effect :highlight, "help_#{uniqid}"
    end + content_tag(:div,:class=>"help",:id=>"help_#{uniqid}", :style=>"display:none") do
      yield
    end )
  end
end