Rails 4: displaying stars issue with coffeescript

2019-09-19 16:03发布

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>

1条回答
成全新的幸福
2楼-- · 2019-09-19 16:23

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.

查看更多
登录 后发表回答