I am using the following helper
to add is_active
css class to active links in my navigation bar.
# 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
This code works perfectly. However, I want to keep is_active
class on every page which related to the controller. For example, here is my controller route:
# routes.rb
resources :catalogs, path: "s", only: [:index, :show] do
resources :products, path: "", only: [:show]
end
and imagine that I have 100 different catalogs. I want to keep is_active
on the index page, while the user switches show pages.
Link in my navigation bar:
= active_link_to "Products", catalog_path(1), class: "navbar_link"
Please note, that in the previous line I am navigation to the very first catalog, not to the index
page.
Thank you for your help and time!
You can try active_link_to gem, which does exactly what you want. It would be something like this (not tested)
active_link_to 'Products', catalog_path(1), active: [['catalogs'], ['index', 'show']], class_active: 'is_active'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.