How to get the current page? to match multiple actions?

My problem is that I'm trying to do something like

current_page?(controller: 'tigers', action:('index'||'new'||'edit')) 

which returns true when the controller is a tiger, and the action is either an index, or a new one, or an edit.

The above error does not produce, but corresponds only to the first action.

Help!

+7
ruby-on-rails haml
source share
3 answers

More detailed, but works:

 current_page?(controller:'bikes') && (current_page?(action:'index') || current_page?(action:'new') || current_page?(action:'edit')) 

Less verbose also works:

 params[:controller] == 'bikes' && %w(index new edit).include?(params[:action]) 

% w () is a shortcut for building space-delimited string arrays

+11
source share

Alternative to using current_page? the method is to directly check the parameter hash:

 params[:action] == ('index' || 'new' || 'edit') 

Will return true if indexed, new, or edited. You can also access the controller via params [: controller].

+4
source share

You can also do something like this:

 if current_page?(root_path) || current_page?(about_path) || current_page?(contact_path) || current_page?(faq_path) || current_page?(privacy_path) 

Note. Do not leave empty space in front of the bracket, otherwise it will not work.

+3
source share

All Articles