rails content_tag nav builder helper

时间:2012-01-27 11:01:27

标签: ruby-on-rails helpers content-tag

很抱歉,如果这听起来很明显,但我感到很困惑。 我正在尝试构建一个导航列表助手。

目前它收到一个数组链接列表。

module ApplicationHelper
  def site_pages
    %w{index about contact promotions}
  end

  def nav_builder list     
    list.map do |l|
      content_tag :li do
        content_tag :a, :href => "#{l}_path" do
          l
        end
      end
    end
  end
end

但是当我运行页面时,它会输出所有页面作为数组的页面。

[<li><a href="index_path">index</a></li> <li><a href="about_path">about</a></li> <li><a href="contact_path">contact</a></li> <li><a href="promotions_path">promotions</a></li>]

而不是显示链接列表。 有什么特别的事吗?

== EDIT ==

我这样打电话给我的建设者:

<%= nav_builder site_pages %>

1 个答案:

答案 0 :(得分:2)

帮助程序应该返回一个HTML字符串,而不是一个数组。尝试将Array元素连接到一个String:

def nav_builder list     
  list.map do |l|
    content_tag :li do
      content_tag :a, :href => "#" do
        "test"
      end
    end
  end.join("\n").html_safe
end
相关问题