use*_*621 10 ruby haml if-statement ruby-on-rails-3
我需要在我的HAML代码中使用这种结构:
- if something1
%div.a
- elsif something2
%div.b
- elsif something3
%div.c
- else
%div.d
%div another content
Run Code Online (Sandbox Code Playgroud)
我希望我会得到类似的东西:
<div class="a|b|c|d">
<div>another content</div>
</div>
Run Code Online (Sandbox Code Playgroud)
但事实上我得到了
<div class="a|b|c|d"></div>
<div>another content</div>
Run Code Online (Sandbox Code Playgroud)
如果需要,我必须如何更新我的代码:
另一个内容?
Get*_*Set 10
@ Ingenu的助手方法看起来更聪明,但如果你不介意它更快更脏,你可以这样做:
- if something1
-divclass = 'a'
- elsif something2
-divclass = 'b'
- elsif something3
-divclass = 'c'
- else
-divclass = 'd'
%div{:class => divclass}
%div another content
Run Code Online (Sandbox Code Playgroud)
我认为你应该创建一个帮助方法:
%div{:class => helper_method(useful_parameters)}
Run Code Online (Sandbox Code Playgroud)
实现这一点的真正丑陋的方法是使用三元运算符(condition ? true_case : false_case),这听起来不是一个很好的解决方案,因为你选择了haml并且想让你的代码库干净.