2

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

Kirinriki
  • 855
  • 4
  • 12
  • 18

1 Answers1

5

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

Thilo
  • 17,565
  • 5
  • 68
  • 84
  • @ Thilo: Oh, wow, thanks for this input. Now other 2 questions: **QUESTION 1:** Where to put the picture of the black and grey star so rails will find it? In the same folder like my html.erb? **QUESTION 2:** okay, so @model is my table, right? I edited my first post with information about my model because it's quite more complex. Now I simply wanted to view the black stars in my example: `<% @employee.position.expected_qualifications.each do |expected_qualification| %>

    <%= expected_qualification.qualificationstars.times { image_tag("black_star.png") } %> <%end%>`

    – Kirinriki Jul 17 '11 at 11:01
  • at least there was no error, but it also didn't show any pictures- just the numbers. – Kirinriki Jul 17 '11 at 11:02
  • BTW, @model in not your table. It's a placeholder for the instance of your class that has the stars attribute since I didn't know your class names. So in your case, @employee.position.expected_qualification and @employee.current_qualification, respectively. – Thilo Jul 17 '11 at 11:20
  • My bad. Fixed the blocks, try this version. If somebody knows a one line version of the 3 line blocks, please chime in. – Thilo Jul 17 '11 at 11:26
  • thanks alot for your effort! Yes, I think I'll rename "qualificationstars" into "stars" next time, you're right. I implemented your code and it's so logical (for me), but rails gives me a `noMethodError`: undefined method `current_qualification' for #. So it seems it has some problems with the times-loop? – Kirinriki Jul 17 '11 at 11:41
  • Do you have :has_one current_qualification in your Employee model? Or is it a :has_many? Then you need to select the qualification you want to compare, e.g. @employee.current_qualifications.first, or loop through all of them. If neither works, post the relevant sections of your models. – Thilo Jul 17 '11 at 11:46
  • It's a :has_many association (as you can see in the picture), because both tables are additionally a join table. For example the **expected_qualification table** is a join table between position and skills: a position has many skills and a skill belongs to many positions. Same for the **current_qualification table**: An employee has many skills and a skill has many employees. – Kirinriki Jul 17 '11 at 11:49
  • Good news! with a little bit modification, it seems like it's working now. I edited my first post. – Kirinriki Jul 17 '11 at 12:21
  • One line version could be something like `<%= raw image_tag("black_star.png") * @employee.current_qualification.qualificationstars %>` since you can multiply a string by a number x to repeat it x times, no? – Nathan Oct 10 '12 at 01:16