Concat in View Helpers

A common technique to simplify logic in views is to use helpers to define blocks that conditionally include content. For instance a helper to only include a block if the user is logged in could look like:

module ApplicationHelper
  def logged_in_block(&block)
    if logged_in?
      concat content_tag(:div, capture(&block))
    end
  end
end

and would be used like this in a view

<% logged_in_block do %>
  <div id="user-bar-greeting">Logged in as ... </div>
<% end %>

In Rails 3.0, this results in the block being included twice. The new convention is that if a Ruby code block in an erb is going to output something, if should be coded as <%= %>. In this case the concat method is no longer necessary. The content_tag method can directly take a block as a parameter as well.

This can be simplified to

def logged_in_block(&block)
  content_tag(:div, &block) if logged_in?
end

and used

<%= logged_in_block do %>
  <div id="user-bar-greeting">Logged in as ... </div>
<% end %>