JQuery + thumbs_up gem render vote count?

前端 未结 1 1363
夕颜
夕颜 2021-02-11 03:25

Plugins: Thumbs Up & JQuery 1.5.2 (needed for another old gem)

I\'m trying to render an updated vote count w/o a full HTTP request when a user votes on a post. Curre

相关标签:
1条回答
  • 2021-02-11 04:10

    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! :)

    0 讨论(0)
提交回复
热议问题