3

i have a Rails app in which I am using jquery Raty plugin i have included Raty in gemfile

gem 'jquery-raty-rails', github: 'bmc/jquery-raty-rails'

and in the application.js i have included

//= require jquery.raty
//= require jquery.raty.min

and i have included this in the javascript

  $('#star-log').raty({
    target     : '#hint-log',
    targetType : 'score',
    targetKeep : true
});
$('#star-comm').raty({
    target     : '#hint-comm',
    targetType : 'score',
    targetKeep : true
});
$('#star-tech').raty({
    target     : '#hint-tech',
    targetType : 'score',
    targetKeep : true

});
$('#star-overall').raty({
    target     : '#hint-overall',
    targetType : 'score',
    targetKeep : true,
    readOnly   : true
});

the view for the stars rating is given as

<div class="row">
            <div class=" col s12 m6 logical">
              <label>Logical & Analytical Skills</label>
              <div id="star-log" > </div>
              <%= f.text_field :log_skill, :id=>'hint-log' %>

            </div>
            <div class=" col s12 m6">
              <label>Communication skills</label>
              <div id="star-comm" ></div>
              <%= f.text_field :comm_skill, :id=>'hint-comm' %>
            </div>
          </div>
                <div class="row">
                  <div class=" col s12 m6">
                    <label>Technical Skills</label>
                    <div id="star-tech" id="star-tech"></div>
                    <%= f.text_field :log_skill, :id=>'hint-tech' %>
                  </div>
                  <div class="col s12 m6">
                    <label >Overall Rating</label>
                    <div id="star-overall" id="star-read"></div>
                    <%= f.text_field :log_skill, :id=>'hint-overall' %>
                  </div>
                </div>

I have 4 fields for star rating given as

  1. Logical & Analytical Skills
  2. communication skills
  3. technical skills
  4. overall skill

so now in first three star rate the user will rate and by those ratings the overall skill(which is read only) will be updated while rating or we can say overall skill rating will be the average of first three skills and it will automatically being updated and keep showing the stars How can i do this ?

user4965201
  • 973
  • 1
  • 11
  • 25

1 Answers1

3

Add class stars to group 3 of the star ratings

<div class="row">
  <div class=" col s12 m6 logical">
    <label>Logical & Analytical Skills</label>
    <div id="star-log" class="stars" > </div>
    <%= f.text_field :log_skill, :id=>'hint-log' %>
  </div>

  <div class=" col s12 m6">
    <label>Communication skills</label>
    <div id="star-comm" class="stars" ></div>
    <%= f.text_field :comm_skill, :id=>'hint-comm' %>
  </div>
</div>
<div class="row">
  <div class=" col s12 m6">
    <label>Technical Skills</label>
    <div id="star-tech" class="stars"></div>
    <%= f.text_field :log_skill, :id=>'hint-tech' %>
  </div>
  <div class="col s12 m6">
    <label >Overall Rating</label>
    <div id="star-overall"></div>
    <%= f.text_field :log_skill, :id=>'hint-overall' %>
  </div>
</div>

Removed double id given to last to star ratings (star tech and overvall)

$('#star-log').raty({
    target     : '#hint-log',
    targetType : 'score',
    targetKeep : true
});

$('#star-comm').raty({
    target     : '#hint-comm',
    targetType : 'score',
    targetKeep : true
});

$('#star-tech').raty({
    target     : '#hint-tech',
    targetType : 'score',
    targetKeep : true

});

$('#star-overall').raty({
    target     : '#hint-overall',
    targetType : 'score',
    targetKeep : true,
    readOnly   : true
});

$(document).on("click", ".stars", function(){
  var score = 0 ;

  //loop through stars to get score
  $('.stars').each(function(i, obj) {
    //if score is there will be undefined if star not selected
    if ($(obj).raty('score')) 
      score +=  $(obj).raty('score'); 
  });
 //calculating average
 score = score / $(".stars").length;
 $('#star-overall').raty({score:  score });
 $("#hint-overall").val(score.toFixed(2));
});
Pardeep Dhingra
  • 3,916
  • 7
  • 30
  • 56
  • one more question, all the star ratings except overall rating are showing the target score but overall rating is not showing the target score while updating also when i set score : 4 to overall rating it then shows the target score – user4965201 Jun 04 '15 at 15:32
  • @nishantvarshney is there any specific reason you are using 2 text_field with same model attribute name – Pardeep Dhingra Jun 04 '15 at 15:53
  • @nishantvarshney check updated answer..for adding score in textbox too..`$("#hint-overall").val(score.toFixed(2));`. Target will not work in this case as we are not triggering event on that star rating item. – Pardeep Dhingra Jun 04 '15 at 16:04
  • i am using it to show the over all score in the text field . actually the main motive is to show as well as to store in the f.text_field so that it can be saved in the database after submit , is there any other way to store in the database ? – user4965201 Jun 04 '15 at 17:49
  • ohh i am sorry the log_skill is repeated 2 times ... actually the other 2 are :tech_skill and :overall_skill(the table fields).. – user4965201 Jun 04 '15 at 17:56