-1

I've asked another question yesterday about issues with javascript. I have gem 'jquery-turbolinks', '~> 0.2.1' installed.

The problem is that the rating for post is not showing(displaying stars). It worked fine few days ago, but now it's just not displaying stars.

ratings.coffee

ready = ->
  $("form[data-update-target]").bind "ajax:success", (evt, data) ->
    target = $(this).data("update-target")
    $("#" + target).prepend data
    $('#content').val('')

  $('.rating-total').each () ->
    score = $(this).data('score')
    $(this).raty({
      haftShow: true,
      score: score,
      readOnly: true
    })

$(document).ready(ready)
$(document).on('page:load', ready)

file for displaying stars

<div class="rating-total" data-id= <%= post.id %> data-score= <%= avg_score_for(post) %> >
</div>

   <div class="total-score">
      <%= avg_score_for post %>
   </div> 

html output

<div class="rating-total" data-id= "1" data-score= "3.0" >
</div>

<div class="total-score">
   3.0
</div>
Community
  • 1
  • 1
Absurdim
  • 233
  • 3
  • 15
  • Please, learn how to debug your applications. Compare the actual output with what is expected. In this case you could have easily looked out the generated markup and seen that the data attributes don't have values using the inspector in any mainstream browser. – coreyward May 05 '14 at 17:16

1 Answers1

0

Do you insert or update the star container via ajax (aside turbolinks)? Then you would need to initialize raty on this which you now only do on page load.

We render the stars directly and update the score and vote count like this:

initRatings: ->
  $(".ajax_rating").each (i, el) ->
    $(el).raty
      readOnly: -> $(el).data("readonly") == true
      score: -> $(el).data("rating")
      click: (score, evt) ->
        $.ajax
          url: $(el).data("rateUrl").replace(":score", score)
          success: (data) ->
            $(el).raty("score", data.rating)
            $("#ajax_rating_count").html("(#{data.count})")

Our controller just returns a json with rating and vote count.

2called-chaos
  • 3,018
  • 3
  • 21
  • 29
  • You can rate it only on show. Displaying is for index - you can't rate it on index template. There is no vote count, just stars. – Absurdim May 04 '14 at 17:15