Rails 4:用coffeescript 显示星星问题 [英] Rails 4: displaying stars issue with coffeescript

查看:43
本文介绍了Rails 4:用coffeescript 显示星星问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问了另一个问题 昨天关于 javascript 的问题.我安装了 gem 'jquery-turbolinks','~> 0.2.1'.

问题是帖子的评分没有显示(显示星星).几天前它运行良好,但现在它只是不显示星星.

评分.咖啡

ready = ->$("form[data-update-target]").bind "ajax:success", (evt, data) ->目标 = $(this).data("更新目标")$("#" + target).前置数据$('#content').val('')$('.rating-total').each() ->score = $(this).data('score')$(this).raty({haftShow:真实,分数:分数,只读:真})$(文件).ready(ready)$(document).on('page:load', ready)

显示星星的文件

数据分数= <%= avg_score_for(post) %>>

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

html 输出

<div class="total-score">3.0

解决方案

您是否通过 ajax 插入或更新星形容器(turbolinks 除外)?然后,您需要对此初始化 rate,而您现在仅在页面加载时执行此操作.

我们直接渲染星星并像这样更新分数和投票数:

initRatings: ->$(".ajax_rating").each (i, el) ->$(el).raty只读:->$(el).data("readonly") == true得分:->$(el).data("评级")点击:(分数,evt)->$.ajaxurl: $(el).data("rateUrl").replace(":score", score)成功:(数据)->$(el).raty("score", data.rating)$("#ajax_rating_count").html("(#{data.count})")

我们的控制器只返回一个带有评分和投票数的 json.

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>

解决方案

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.

这篇关于Rails 4:用coffeescript 显示星星问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆