本文介绍了如何仅显示返回为>的link_to助手. 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此帮助链接

link_to "", product_path(product, anchor: "disqus_thread"), data: { "disqus-identifier" => "#{url_for([product, {only_path: false}])}" }, class: "no-underline bold grey-text text-darken-3 margin-left"

布局:application.rb

layout: application.rb

%script{id: "dsq-count-scr", src: "https://url.disqus.com/count.js", async: "async"}

_disqus.html.erb

_disqus.html.erb

<div class="col-lg-8 col-lg-offset-2 big-top-space margin-bottom">
  <div id="disqus_thread"></div>
  <script>
    var disqus_shortname = 'yourname';
    var disqus_identifier = '<%= url_for([product, {only_path: false}]) %>';
    var disqus_title = '<%= product.name %>';
    var disqus_url = '<%= url_for([product, {only_path: false}]) %>';
  (function() { // DON'T EDIT BELOW THIS LINE
     var d = document, s = d.createElement('script');
     s.src = 'https://url.disqus.com/embed.js';
     s.setAttribute('data-timestamp', +new Date());
     (d.head || d.body).appendChild(s);
  })();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>

</div>

一切正常,我得到一个数字,该数字是索引视图中每个帖子的Disqus评论计数器,但是如果大于0,如何仅显示数字?如果等于0 ,我不想在视图中显示它.有人知道如何解决这个问题吗?非常感谢

and all works fine I get a number, the number is the disqus comments counter of each post in my index view, but how show only the number if is greater than 0? if is equal to 0 I dont wanna show it in the view. Someone know how to resolve this problem? Thanks a lot

我尝试过这个:

我更改了_disqus.html

i changed my _disqus.html

 <div class="col-lg-8 col-lg-offset-2 big-top-space margin-bottom">
      <div id="disqus_thread"></div>
      <script>
        var disqus_shortname = 'yourname';
        var disqus_identifier = '<%= url_for([product, {only_path: false}]) %>';
        var disqus_title = '<%= product.name %>';
        var disqus_url = '<%= url_for([product, {only_path: false}]) %>';

       var disqus_config = function () {
    this.callbacks.onNewComment = [
      function() {        
        $.ajax({
          method: "PATCH",
          url: '<%= product_path(product) %>',
          data: {increment: "comment_count"}
        })
      }
    ];
};
      (function() { // DON'T EDIT BELOW THIS LINE
         var d = document, s = d.createElement('script');
         s.src = 'https://url.disqus.com/embed.js';
         s.setAttribute('data-timestamp', +new Date());
         (d.head || d.body).appendChild(s);
      })();
    </script>
    <noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>

    </div>

Product_controller

Product_controller

def update    
    product = Product.find(params[:id])
    product.update(update_product)
  end
def update_product
  params.permit(:comment_count)
end    

来源: https://help.disqus.com/customer/portal/articles/466258-capturing-disqus-commenting-activity-via-callbacks

但我收到此错误

   Started PATCH "/products/12" for ::1 at 2017-04-12 17:44:38 -0500
Processing by ProductsController#update as */*
Parameters: {"increment"=>"comment_count", "id"=>"12"}
ShoppingCart Load (0.0ms) SELECT "shopping_carts".* FROM "shopping_carts" WH
ERE "shopping_carts"."id" = ? LIMIT 1 [["id", 102]]
Product Load (0.0ms) SELECT "products".* FROM "products" WHERE "products"."i
d" = ? LIMIT 1 [["id", 12]]
CACHE (0.0ms) SELECT "products".* FROM "products" WHERE "products"."id" = ?
LIMIT 1 [["id", "12"]]
Unpermitted parameters: increment, id
(0.0ms) begin transaction
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT
1 [["id", 1]]
(0.0ms) commit transaction
Rendered products/update.haml within layouts/application (0.0ms)
(0.0ms) SELECT COUNT(*) FROM "products" INNER JOIN "in_shopping_carts" ON "p
roducts"."id" = "in_shopping_carts"."product_id" WHERE "in_shopping_carts"."shop
ping_cart_id" = ? [["shopping_cart_id", 102]]
Rendered partials/_unlogged.haml (15.5ms)
Rendered partials/_nav.haml (765.8ms)
Completed 200 OK in 3476ms (Views: 3448.8ms | ActiveRecord: 0.0ms)

在控制台中

有人可以帮助我吗?

推荐答案

我认为解决此问题的最佳方法是:

The best way I can think to solve this is to:

  • 向您的数据库添加comment_count属性
  • 添加disqus onNewComment回调以更新记录中的评论计数.
  • add a comment_count attribute to your database
  • add a disqus onNewComment callback to update the comment count on your record.

使用Java语言,您可以添加回调:

With Javascript you can add a callback:

var disqus_config = function () {
    this.callbacks.onNewComment = [
      function() {
        alert(comment.id);
        alert(comment.text);
        $.ajax({
          method: "PATCH",
          url: "<%= product_path(product) %>",
          data: {increment: "comment_count"}
        })
      }
    ];
};

那么您将知道以后的评论数.

then you would know the comment count going forward.

文档: https://help.disqus.com/customer/portal/articles/466258-capturing-disqus-commenting-activity-via-callbacks

将您的控制器更改为:

def update    
  product = Product.find(params[:id])
  if params[:comment_count]
    product.increment!(:comment_count)
  else
    product.update(update_product)
  end
end

这篇关于如何仅显示返回为&gt;的link_to助手. 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 15:31