Rails content_tag 导航生成器助手

发布于 2024-12-29 05:19:00 字数 819 浏览 2 评论 0原文

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

目前,它从数组接收链接列表。

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

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

==编辑==

我这样称呼我的构建者:

<%= nav_builder site_pages %>

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 %>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗恋未遂 2025-01-05 05:19:00

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

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文