简体   繁体   English

如何测试当前页面是否在给定的路径/控制器中?

[英]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 . 我有以下视图,并且如果/tracking_categories路径内的任何地方都想突出显示该按钮,例如: http://localhost:3000/tracking_categories/6/trackings/new I tried current_page? 我尝试了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 我也尝试了#url

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

They all return false, except url , which gives the error 它们都返回false,但url除外,这会导致错误

wrong number of arguments (0 for 2) 参数数目错误(0表示2)

You can use params[:action] and params[:controller]. 您可以使用params [:action]和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}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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