current_page可能吗?拿两个论点?

时间:2013-12-22 12:54:13

标签: ruby-on-rails ruby-on-rails-4

我正在尝试执行以下操作,但它不起作用:

class="<%= current_page?(projects_path || new_project_path) ? 'current' : '' %>"

Rails不会出错,它只是忽略了OR运算符右侧的内容。

是否可以为current_page?提供多个可选网址?

The docs doesn't give me an answer.


1 个答案:

答案 0 :(得分:1)

如果projects_path || new_project_path不为零,则表达式projects_path将始终返回projects_path

由于始终会定义projects_pathnew_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"
相关问题