«if statement» for nested routes

谁都会走 提交于 2019-12-12 05:40:06

问题


I have nested routes in my routes.rb

resources :companies do
  resources :employees
  resources :accounts
end

In top menu I want to show a certain link when user is in company's controller or in nested controllers (employees, accounts). So, I want a simple «if statement» for it.

I've tried several approaches.

<%= if params[:company_id].present? %> # doesn't work in company views
  # certain link
<% end %>

<%= if current_page?(controller: 'companies') %> # doesn't work in nested controllers' views
  # certain link
<% end %>

Of course, it is possible to use both of them with or, but I think it can be a better way for this.

Thanks!


回答1:


You could use controller_name:

<% if controller_name.match(/^companies/) %>
    # certain link
<% end %>

UPDATE
Alternatively, use controller_path:

<% if controller_path.match(/^companies/i) %> 
    # certain link
<% end %>


来源:https://stackoverflow.com/questions/29835029/if-statement-for-nested-routes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!