[英]How can I test if the current page is in a given path/controller?
我有以下路線
resources :tracking_categories do
resources :trackings
end
我有以下視圖,並且如果/tracking_categories
路徑內的任何地方都想突出顯示該按鈕,例如: http://localhost:3000/tracking_categories/6/trackings/new
。 我嘗試了current_page?
%li{class: current_page?(controller: 'tracking_categories') ? 'active' : false}
=link_to 'Track My Progress', new_tracking_category_tracking_path(1)
我也試過
%li{class: current_page?(controller: 'trackings') ? 'active' : false}
我也嘗試了#url
%li{class: url.index('tracking_categories') ? 'active' : false}
它們都返回false,但url
除外,這會導致錯誤
參數數目錯誤(0表示2)
您可以使用params [:action]和params [:controller]。
這是一個類似的問題,建議您可以使用:
controller.controller_name == "the_controller_I_want_to_check"
controller.action_name == "the_action_I_want_to_check"
這工作
%li{class: request.original_url.index('tracking_categories') ? 'active' : false}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.