span 标记当前如下所示:
<span class='best_in_place<%= ' list-downvoted' if upload.downvoted? %><%= ' list-upvoted ' if upload.upvoted? %>'
但是,如果此跨度既被否决又被点赞,则它应用这两种样式。
我如何修改它以仅应用一个,如果另一个确实存在,则删除另一个?
即,如果此项目之前被否决,则删除list-downvoted
类,反之亦然。
编辑:在我的UploadsHelper
中添加了推荐的代码
module UploadsHelper
def best_span_class_for_upload(upload)
# Start with the base style
css_class = 'best_in_place'
# Add on any conditional styles using String#<< concatenation
css_class << ' list-downvoted' if (upload.downvoted?)
css_class << ' list-upvoted' if (upload.upvoted?)
# Return the result
css_class
end
end
跨度现在看起来像这样:
<span class='<%= best_span_class_for_upload(upload) %>' id='best_in_place_upload_name' data-url='<%= upload_path(upload) %>' data-object='upload' data-attribute='name' data-formType='input' data-activator='span#upload-edit-name-<%= upload.id %>'><%= upload.name %></span>
最好汇总
到一个帮助程序方法中,以避免弄乱模板:
<span class='<%= best_span_class_for_upload(upload) %>'>
然后,在helper/
定义的模块中定义的帮助程序方法将如下所示:
def best_span_class_for_upload(upload)
# Start with the base style
css_class = 'best_in_place'
# Add on any conditional styles using String#<< concatenation
css_class << ' list-downvoted' if (upload.downvoted?)
css_class << ' list-upvoted' if (upload.upvoted?)
# Return the result
css_class
end
更新:
仅显示向上或向下之一的修订版本:
def best_span_class_for_upload(upload)
# Start with the base style
css_class = 'best_in_place'
# Add on any conditional styles using String#<< concatenation
if (upload.downvoted?)
css_class << ' list-downvoted'
elsif (upload.upvoted?)
css_class << ' list-upvoted'
end
# Return the result
css_class
end