简体   繁体   中英

How can I test if the current page is in a given path/controller?

I have the following routes

  resources :tracking_categories do
    resources :trackings
  end

I have the following view and I'd like to highlight the button if anywhere inside the /tracking_categories path, ex: http://localhost:3000/tracking_categories/6/trackings/new . I tried current_page?

  %li{class: current_page?(controller: 'tracking_categories') ? 'active' : false}
    =link_to 'Track My Progress', new_tracking_category_tracking_path(1)

I also tried

%li{class: current_page?(controller: 'trackings') ? 'active' : false}

I also tried #url

  %li{class: url.index('tracking_categories') ? 'active' : false}

They all return false, except url , which gives the error

wrong number of arguments (0 for 2)

You can use params[:action] and params[:controller].

Routing parameters documentation

Here is a similar question suggesting that you can use:

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}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM