我正在尝试执行以下操作,但它不起作用:
class="<%= current_page?(projects_path || new_project_path) ? 'current' : '' %>"
Rails不会出错,它只是忽略了OR
运算符右侧的内容。
是否可以为current_page?
提供多个可选网址?
The docs doesn't give me an answer.
答案 0 :(得分:1)
如果projects_path || new_project_path
不为零,则表达式projects_path
将始终返回projects_path
。
由于始终会定义projects_path
和new_project_path
路径,因此当您执行此操作时,您将始终将projects_path
传递给current_page?
方法:
current_page?(projects_path || new_project_path)
将您的代码更新为:
class="<%= (current_page?(projects_path) || current_page?(new_project_path)) ? 'current' : '' %>"
示例:
> 'test' || nil
=> "test"
> nil || 'test'
=> "test"
> 'test' || 'test2'
=> "test"