如何使这个ruby方法不那么难看(嵌套)

JRL*_*JRL 1 ruby coding-style nested ruby-on-rails

我有一个帮助方法,为一些控制器创建导航链接.

  def gen_associations(controllers)
    content_for :leftnav do
      sorted_controllers = controllers.sort
      returning String.new do |content|
        content << content_tag(:h3, "Associations") <<
        content_tag(:ul, :class => "nav") do
          sorted_controllers.collect do |c|
            content_tag("li", :class => ("last" if c == sorted_controllers.last)) do
              link_to(c.humanize, eval("admin_#{c}_url"))
            end
          end
        end
      end
    end
  end
Run Code Online (Sandbox Code Playgroud)

我不喜欢这种深层嵌套的结构,以及<<其中一条线的额外和结束.

我怎样才能重写它所以它不是那样嵌套(在更少的行中)和没有长行(<80个字符)?

tig*_*tig 7

使用部分 - 将所有内容放入返回的闭包中然后使用 render :partial => ...