[英]Is a Rails Model a normal ruby class that can be used to store state between different controller actions? No active-record and no-database at all
[英]Add active class to all relative actions inside controller Ruby on Rails
我正在使用以下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_active
類。 例如,這是我的控制器路線:
# routes.rb
resources :catalogs, path: "s", only: [:index, :show] do
resources :products, path: "", only: [:show]
end
想象我有100個不同的目錄。 我想在索引頁面上保持is_active
,而用戶切換顯示頁面。
我的導航欄中的鏈接:
= active_link_to "Products", catalog_path(1), class: "navbar_link"
請注意,在上一行中,我導航到第一個目錄,而不是index
頁面。
感謝您的幫助和時間!
您可以嘗試active_link_to gem,它完全可以滿足您的需求。 可能是這樣(未經測試)
active_link_to 'Products', catalog_path(1), active: [['catalogs'], ['index', 'show']], class_active: 'is_active'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.