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.
当控制器是老虎并且操作是索引,新建或编辑时,它返回true。
The above does not throw an error, but only matches the first action.
以上不会引发错误,只会匹配第一个操作。
Help!
3 个解决方案
#1
12
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
%w()是构建空格分隔字符串数组的快捷方式
#2
4
An alternative to using the current_page? method is to check the params hash directly:
使用current_page的替代方法?方法是直接检查params哈希:
params[:action] == ('index' || 'new' || 'edit')
Will return true if on index, new, or edit. You can also access the controller through params[:controller].
如果在索引,新建或编辑时返回true。您也可以通过params [:controller]访问控制器。
#3
3
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.
注意:不要在括号前留空格,否则不起作用。
#1
12
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
%w()是构建空格分隔字符串数组的快捷方式
#2
4
An alternative to using the current_page? method is to check the params hash directly:
使用current_page的替代方法?方法是直接检查params哈希:
params[:action] == ('index' || 'new' || 'edit')
Will return true if on index, new, or edit. You can also access the controller through params[:controller].
如果在索引,新建或编辑时返回true。您也可以通过params [:controller]访问控制器。
#3
3
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.
注意:不要在括号前留空格,否则不起作用。