2

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)

Chloe
  • 25,162
  • 40
  • 190
  • 357

2 Answers2

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"
Community
  • 1
  • 1
coding addicted
  • 3,422
  • 2
  • 36
  • 47
0

This worked

%li{class: request.original_url.index('tracking_categories') ? 'active' : false}
Chloe
  • 25,162
  • 40
  • 190
  • 357