简体   繁体   English

遍历记录Rails助手

[英]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. 我的助手如何显示一个数组,即使我正在使用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. 而不是content_tag列表项。

["<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. 我尝试了.html_safe等,但是看起来我缺少了一些东西。

It is returning an Array. 它正在返回一个数组。 You can try this: 您可以尝试以下方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM