Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get 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 tigers and the action is either index, new, or edit.

The above does not throw an error, but only matches the first action.

Help!

like image 883
atonyc Avatar asked Feb 01 '14 00:02

atonyc


3 Answers

More verbose, 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 arrays of space delimited strings

like image 184
sbecker Avatar answered Oct 19 '22 17:10

sbecker


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 blank space before parenthesis otherwise it won't work.

like image 27
cyonder Avatar answered Oct 19 '22 17:10

cyonder


An alternative to using the current_page? method is to check the params hash directly:

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

Will return true if on index, new, or edit. You can also access the controller through params[:controller].

like image 2
FCStrike Avatar answered Oct 19 '22 19:10

FCStrike