JQuery + thumbs_up gem render vote count?

╄→гoц情女王★ 提交于 2019-12-03 09:04:55

It seems like you'll want to separate out your view code into partials and only refresh one partial when a rating is provided.

For your controller, instead of:

respond_to do |format|
    format.js
    format.html {redirect_to :back}
  end

Do something like:

render :partial => "voutecount"

In your view, move out the votewrapper div into a new file called "_votecount.html.erb" within the same directory, and instead have the render code:

 <%= render :partial => "votecount" %>

If you want to block the rating while it's refreshing (recommended), then you may want to ajaxify the call and control it more in the js. So, include your javascript within the view:

<%= javascript_include_tag 'votecount' %>

replace your link_to with good ol' html to have more info:

<a href="" class="ratelink" updown="up" theid="123"><img src = "...."></a>
<a href="" class="ratelink" updown="down" theid="123"><img src = "...."></a>

And create a new votecount.js in your public/javascripts folder with the following content:

  $(function(){
        $(".ratelink").click(function(){
            var val = $(this).attr('updown');
            var theid = $(this).attr('theid');
            $("#votewrapper").block({ //blocks rate-rates while processing
                message: null,
                overlayCSS: {
                    backgroundColor: '#FFF',
                    opacity: 0.6,
                    cursor: 'default'
                },
            });
        if (val == "up") {
        $.ajax({
                type: 'PUT',
                url: "/mymodel/voteup?id="+theid,
                success: function(){
                            $("#votewrapper").unblock();
                            }   
                   });
        } else {
             $.ajax({
                type: 'PUT',
                url: "/mymodel/votedown?id="+theid,
                success: function(){
                            $("#votewrapper").unblock();
                            }   
                   });
        }
    })

good luck! :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!