ruby-on-rails - 如何在子元素上设置 active_link_to 以仅设置为活动?
问题描述
我是 Rails 新手,我只想设置active_link_to
子元素。我正在使用这个 gem https://github.com/comfy/active_link_to
情况:
parent element path:
localhost:3000/columns // This one is set to active when going to that specific page
child element path:
localhost:3000/columns/post // This one will also be active also but the parent element is also active
我有这个导航栏:
<ul class="nav-bar">
<li class="nav-item">
<%= active_link_to columns_root_path, :class => 'nav-link' do %>
Columns
<% end %>
</li>
<li class="nav-item">
<%= active_link_to columns_post_path, :class => 'nav-link' do %>
Post
<% end %>
</li>
</ul>
active_link_to
如果我只去子元素,如何删除父元素中的。因为在我的情况下,当我走这条路时:and路径都localhost:3000/columns/post
将设置为活动状态。columns
post
我的路线文件中有这里。 路线.rb
namespace :columns do
root 'columns#index', as: "root"
get 'post' => 'post#index'
end
通常我将文件夹设置为这个:
app/views/columns/columns/index.html.erb //for the columns index
app/views/columns/post/index.html.erb //for the post index
解决方案
我对active_link_to
宝石不熟悉。但是重新阅读文档,我猜这个active: :exact
选项就是你要找的。
<ul class="nav-bar">
<li class="nav-item">
<%= active_link_to columns_root_path, active: :exact, :class => 'nav-link' do %>
Columns
<% end %>
</li>
<li class="nav-item">
<%= active_link_to columns_post_path, active: :exact, :class => 'nav-link' do %>
Post
<% end %>
</li>
</ul>
推荐阅读
- c# - 数据表中存在值问题c#
- flutter - 在颤动中解析 JSON 到 Map
- mongodb - 带有 ObjectId 的 Mongo 文档未更新并使用 Mongo.ObjectId 创建新文档
- c# - Atata - 如何在页面类中定义警报?
- php - 函数中的 parse_ini_string () 作为 GLOBAL 变量?
- kubernetes - 在 cronjob.yaml 中添加多个命令
- html - 拆分时向表格添加行
- microsoft-graph-api - 使用 /oauth2/token 生成令牌 MS Azure API 返回 AADSTS50034 错误
- angular - 如何延迟 Angular 8 和 rxjs 6 中的 img 显示?
- c# - 选择索引更改事件上的组合框