css - How do I apply a style to a <span> if a condition exists, and take it away if it doesn't in Rails 3? -


the span tag looks this:

<span class='best_in_place<%= ' list-downvoted' if upload.downvoted? %><%= ' list-upvoted ' if upload.upvoted? %>' 

however, if span both downvoted & upvoted, applies both styles.

how modify apply one, , rid of other if other exist?

i.e. if item has been downvoted before, remove list-downvoted class, , vice versa.

edit: added recommended code in uploadshelper

module uploadshelper     def best_span_class_for_upload(upload)       # start base style       css_class = 'best_in_place'        # add on conditional styles using string#<< concatenation       css_class << ' list-downvoted' if (upload.downvoted?)       css_class << ' list-upvoted' if (upload.upvoted?)        # return result       css_class     end end 

with span looking now:

<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> 

this might better roll helper method avoid making mess of template:

<span class='<%= best_span_class_for_upload(upload) %>'> 

then helper method defined in helper/ defined module this:

def best_span_class_for_upload(upload)   # start base style   css_class = 'best_in_place'    # add on conditional styles using string#<< concatenation   css_class << ' list-downvoted' if (upload.downvoted?)   css_class << ' list-upvoted' if (upload.upvoted?)    # return result   css_class end 

update:

revised version shows 1 of or down:

def best_span_class_for_upload(upload)   # start base style   css_class = 'best_in_place'    # add on conditional styles using string#<< concatenation   if (upload.downvoted?)     css_class << ' list-downvoted'    elsif (upload.upvoted?)     css_class << ' list-upvoted'   end    # return result   css_class end 

Comments

Popular posts from this blog

Cursor error with postgresql, pgpool and php -

c# - how to write client side events functions for the combobox items -

ssh client with paramiko (python) -