gpt4 book ai didi

ruby-on-rails - 在 rails 中更改当前页面导航链接的类

转载 作者:数据小太阳 更新时间:2023-10-29 08:15:14 25 4
gpt4 key购买 nike

抱歉,我不知道标题应该是什么,所以请有人更改它以最适合我的问题。

基本上,我有一个这样的列表:

<li class="active"><%= link_to "Home", root_path %></li>
<li><%= link_to "About", about_path %></li>
<li><%= link_to "Contact", contact_path %></li>

根据我所在的页面(主页、关于、内容),我想要相应的 <li>标签有 class="active"属性。最好的方法是什么?

我有一个类变量@title 设置为正在导航的任何页面。

最佳答案

使用 current_page?(options) 知道 Controller / Action /参数是否对应于 url,并根据它切换类,例如使用这样的助手(未经测试):

 def nav_item(name, path)
if current_page?(path)
@title = name
end
content_tag('li', :class=>(current_page?(path) ? 'active' : nil) ){link_to(name,path)}
end

<%= nav_item 'Home', root_path %>
<%= nav_item 'About', about_path %>
<%= nav_item 'Contact', contact_path %>

关于ruby-on-rails - 在 rails 中更改当前页面导航链接的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8586609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com