Benjamin
Benjamin

Reputation: 2118

Iterate through records rails helper

I am trying to create helper method that displays the navigation. How ever my helper displays an array even though i am using content_tag. Was wondering if i was doing something wrong.

module SubscriberNavigation

  def navigation
    get_menu_items.find_each.map do |menu|
      content_tag(:li, link_to("#{ menu.title.try(:capitalize) }", "#{ menu.url.downcase }"))
    end
  end


  def get_menu_items
    @get_menu_items ||= Subscriber::Menu.all
  end

end

And when i display

<%= navigation %>

An array of records in being displayed. Instead of content_tag list items.

["<li><a href=\"http://demo.lvh.me:3000/contact\">Contacts</a></li>", "<li><a href=\"http://demo.lvh.me:3000/pages/test-page\">Terms and conditions</a></li>", "<li><a href=\"http://demo.lvh.me:3000/pages/terms-and-conditions\">About us</a></li>"]

I tried .html_safe etc but looks like i'm missing something.

Upvotes: 0

Views: 199

Answers (1)

Sachin Prasad
Sachin Prasad

Reputation: 5411

It is returning an Array. You can try this:

<%= navigation.join(' ').html_safe %>

Upvotes: 2

Related Questions