如何将指定nav-item与nav-link代码包裹在link_to代码块中?
Converting Your HTML to a Rails
link_to Block 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 Railslink_tohelper. We pass the target URL ("index.html") and theclass: "nav-link"option to match your original styling exactly. - The
do...endblock 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
相关产品推荐
相关产品推荐

