我需要一个助手来生成一个<li>包含在活动类中的链接。
如果没有支持块,这很容易:
  def nav_item(*args, &block)
    url = args[1]
    clazz = 'active' if current_page?(url)
    content_tag(:li, :class => clazz) do
      link_to(*args)
    end
  end
但就像link_to我希望我的助手支持定义内容的块一样。使用 link_to 我可以:
那么我如何在我的助手中支持相同的呢?
我需要做的就是将块传递给link_to. 我目前的尝试
  def nav_item(*args, &block)
    url = if block_given?
      args.first
    else
      args[1]
    end
    clazz = 'active' if current_page?(url)
    content_tag(:li, :class => clazz) do
      if block_given?
        # What goes here?
      else
        link_to(*args)
      end
    end
  end