ruby change color of label if conditional -


i'am start learn code (and english .. ) , have prob ruby conditional . :o

i have array, " category" depending "publication" post.

and color of label category change in fonction catgeory ?

( sorry english work learn ^^ )

it's part of simple form for post product ( 's false it's show idea )

 <%= f.input :category, collection:[" application ", "nature" ,"design", "science"], prompt: "choisissez votre categorie"%>** 

its part of index view product

<% if category["nature"]? %>  <span class="label label-info">  <h6><%= publication.category %></h6> </span> <% else if category["tech"]? %>  <span class="label label-sucess"> <h6><%= publication.category %></h6> </span> <%end%> 

thx help

an alternative above suggestion create helper method.

def publication_label(category)   colors = {     'nature'      => 'label-info',     'tech'        => 'label-success',     'application' => 'label-info'   }    content_tag(:span, class: "label #{colors[category]}")     content_tag(:h6, category)   end end 

(since new rails, might able put /app/helpers/publications_helper.rb).

with helper in place, view can simple as:

<%= publication_label publication.category %> 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -