Search code examples
ruby-on-railshaml

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!


Solution

  • 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