#40 Blocks in View

If you try to create a helper method which accepts a block, you will run into a few gotchas. Learn the secrets of blocks in views in this episode.
<% side_section do %>
  This is on the side bar.
<% end %>

<% admin_area do %>
  <%= link_to "Edit Task", edit_task_path(@task) %>
<% end %>
# application_helper
def side_section(&block)
  @side_sections ||= []
  @side_sections << capture(&block)
end

def admin_area(&block)
  concat('<div class="admin">', block.binding)
  block.call
  concat("</div>", block.binding)
end

# or...
def admin_area(&block)
  if admin?
    concat content_tag(:div, capture(&block), :class => 'admin'), block.binding
  end
end

你可能感兴趣的:(block)