Search code examples
ruby-on-railsactionview

Rails - view a picture which represents data in the database


I'm searching for some idea how to let the stars view like on this example:

enter image description here

Right now, I have a table with a column for the black stars and a column for the grey stars.(in "Hüllenkontrolle" for example a "2" for 2 black stars and "4" for 4 grey stars). So I thought about something that shows another picture depending on which number is in the database (1 = 1 Star, 2 = 2 stars, 3 = 3 stars, 4 = 4 stars...)

I have no idea what to search on internet or how to realize that not the numbers will be viewed but some stars. Has anybody any idea how to implement this?


some information about my database model: http://img6.imagebanana.com/img/kwenevyz/beziehungen.png

  • all data is viewed in the employee's html.erb.
  • the black stars represent the current_qualification-table (column "qualificationstars"), the grey stars represent the expected qualification-table (column "qualificationstars"). Right now it's written in html.erb like this:

<% @employee.position.expected_qualifications.each do |expected_qualification| %>

<%= expected_qualification.qualificationstars %>

<%end%>


Some good news! This seems to work

<% @employee.position.skills.zip(@employee.position.expected_qualifications, @employee.current_qualifications).each do |skill, expected_qualification, current_qualification| %>
    <p>
    <% (current_qualification.istqualifikation).times do %>
            <%= image_tag("black.png") %>
    <% end %>
    <% (expected_qualification.sollqualifikation - current_qualification.istqualifikation).times do %>  
        <%= image_tag("grey.png") %>
    <% end %>
    </p>
<% end %>

This is how it looks in browser now:

enter image description here


Solution

  • If you have an image file each for a black and grey star, try something like this in your view:

    <% @employee.position.expected_qualifications.each do |expected_qualification| %>
    
    <% @employee.current_qualification.qualificationstars.times do %>
    <%= image_tag("black_star.png") %>
    <% end %>
    
    <% [expected_qualification.qualificationstars - @employee.current_qualification.qualificationstars].times do %>
    <%= image_tag("grey_star.png")  %>
    <% end %>
    
    <% end %>
    

    EDIT:

    The images go into "public/images" (Rails < 3.1) or "app/assets/images" (Rails 3.1)

    EDIT 2: Using your model names.

    (I'd suggest renaming "qualificationstars" to simply "stars" in your DB. qualification.qualificationstars is redundant and a bit cumbersome.)