我在应用程序中递归显示一些嵌套注释,但是在没有嵌套注释时遇到了应用特定类的挑战。仅当nested_commments> 0时,如何才能干净地将类sole
应用于div entry
?
def horizontal_tree_for(comments)
comments.map do |comment, nested_comments|
entry(comment) +
(nested_comments.size > 0 ?
content_tag(:div, horizontal_tree_for(nested_comments), class: "branch") +
"</div>"
: "</div>")
end.join.html_safe
end
def entry(comment)
"<div class='entry' **conditional sole class**><span class='label'> #{comment.name} </span>"
end
如注释中所建议,只需编写该类。
伪代码:
"<div class='entry (nested_comments.size > 0 ? "sole" : "")'>
<span class='label'> #{comment.name} </span>"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句