You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将指定nav-item与nav-link代码包裹在link_to代码块中?

No problem at all! Let's break down how to wrap your navigation item HTML into a Rails link_to do block while keeping all the original styling and content intact:

<li class="nav-item">
  <%= link_to "index.html", class: "nav-link" do %>
    <i class="batch-icon batch-icon-browser-alt"></i> Dashboard
  <% end %>
</li>

Quick breakdown of what's happening here:

  • We keep the original <li class="nav-item"> wrapper since that's just standard HTML structure for your navigation.
  • The <a class="nav-link" href="index.html"> gets replaced with the Rails link_to helper. We pass the target URL ("index.html") and the class: "nav-link" option to match your original styling exactly.
  • The do...end block lets us nest the inner content (the batch icon and "Dashboard" text) just like in your original HTML—this is perfect for when you need link content that's more than just plain text.

Pro tip: If you have a Rails route set up for your dashboard (like a root route or dashboard_path), it's better to use the route helper instead of hardcoding "index.html"—so something like link_to root_path, class: "nav-link" do would be more idiomatic Rails!

内容的提问来源于stack exchange,提问作者foliwe83

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:36:59