rails content_tag nav builder helper

4.6k Views Asked by At

Sorry if this sounds obvious but i'm getting confused. I'm trying to build a navigation list helper.

At the moment it receives a list of links from an array.

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

But when I run the page it ouput everything is the page as an array.

[<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>]

instead of displaying a list of links. Is there anything special to do?

==EDIT==

I call my builder this way:

<%= nav_builder site_pages %>
1

There are 1 best solutions below

4
On BEST ANSWER

The helper is expected to return a String of HTML, rather than an Array. Try joining the Array elements into a single 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