问题
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