嵌套路由的“if语句” [英] «if statement» for nested routes
本文介绍了嵌套路由的“if语句”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 routes.rb中嵌套了路线
resources :companies do
resources :employees
resources :accounts
end
在顶部菜单中,当用户在公司的控制器或嵌套控制器(员工,帐户)中时,我想显示某个链接。所以,我想要一个简单的if语句。
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.
我尝试了几种方法。
<%= 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.
谢谢!
推荐答案
您可以使用controller_name:
You could use controller_name:
<% if controller_name.match(/^companies/) %>
# certain link
<% end %>
UPDATE
或者,使用controller_path:
UPDATE
Alternatively, use controller_path:
<% if controller_path.match(/^companies/i) %>
# certain link
<% end %>
这篇关于嵌套路由的“if语句”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文