使用||在Rails中的Case切换

时间:2023-01-16 12:05:32

I have a partial that I want to display in a layout only when certain pages use that layout. I've set @page_title for all my pages and thought I could use something like this:

我有一部分只有当某些页面使用该布局时我才想在布局中显示。我为我的所有页面设置了@page_title,并认为我可以使用这样的东西:

<% case @page_title when "Log in" || "Forgot Your Password" || "Create a New Password" %><%= render :partial => "common/hello-world" -%><% end -%>

But, the include is only happening on the page titled "Log in" and not the other pages. Are || statements like this not allowed on Case switches? Is there a different way to set an OR statement in the case switch?

但是,包含仅发生在标题为“登录”的页面上,而不是其他页面上。是|| Case开关上不允许这样的语句?是否有不同的方法在案例开关中设置OR语句?

Thanks!

谢谢!

2 个解决方案

#1


21  

This is what you want:

这就是你想要的:

<% case @page_title when "Log in", "Forgot Your Password", "Create a New Password" %><%= render :partial => "common/hello-world" -%><% end -%>

Per http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html#case

根据http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html#case

#2


11  

Use , instead of || to separate the matches after when. See more about Ruby syntax in http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html#case.

使用,而不是||在什么时候分开比赛。有关Ruby语法的更多信息,请参见http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html#case。

#1


21  

This is what you want:

这就是你想要的:

<% case @page_title when "Log in", "Forgot Your Password", "Create a New Password" %><%= render :partial => "common/hello-world" -%><% end -%>

Per http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html#case

根据http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html#case

#2


11  

Use , instead of || to separate the matches after when. See more about Ruby syntax in http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html#case.

使用,而不是||在什么时候分开比赛。有关Ruby语法的更多信息,请参见http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html#case。