我正在使用以下内容
helper
添加
is_active
CSS类到我的导航栏中的活动链接。
# application_helper.rb
def active_link_to(name = nil, options = nil, html_options = nil, &block)
active_class = html_options[:active] || "is_active"
html_options.delete(:active)
html_options[:class] = "#{html_options[:class]} #{active_class}" if current_page?(options)
link_to(name, options, html_options, &block)
end
这个代码工作得很好。但是,我想保留
IS-激活的
与控制器相关的每个页面上的类。例如,这里是我的控制器路径:
# routes.rb
resources :catalogs, path: "s", only: [:index, :show] do
resources :products, path: "", only: [:show]
end
想象一下我有100个不同的目录。我想保持
IS-激活的
在索引页上,当用户切换显示页时。
我的导航栏中的链接:
= active_link_to "Products", catalog_path(1), class: "navbar_link"
请注意,在前一行中,我导航到的是第一个目录,而不是
index
页。
谢谢你的帮助和时间!