如何仅在返回> 0时显示link_to帮助程序

我有这个帮助链接

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" 

layout:application.rb

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

_disqus.html.erb

 
var disqus_shortname = 'yourname'; var disqus_identifier = ''; var disqus_title = ''; var disqus_url = ''; (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); })(); Please enable JavaScript to view the comments powered by Disqus.

并且所有工作正常我得到一个数字,数字是我的索引视图中每个post的disqus评论计数器,但是如果只显示大于0的数字? if 等于0我不想在视图中显示它。 有人知道如何解决这个问题吗? 非常感谢

我试过这个:

将此列添加到产品comment_count:integer

我改变了我的_disqus.html

  
var disqus_shortname = 'yourname'; var disqus_identifier = ''; var disqus_title = ''; var disqus_url = ''; var disqus_config = function () { this.callbacks.onNewComment = [ function() { $.ajax({ method: "PATCH", url: '', 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); })(); Please enable JavaScript to view the comments powered by Disqus.

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) 

未允许的参数:increment,id

在控制台

comment_count:无

有人可以帮帮我吗?

我能想到解决这个问题的最好方法是:

  • comment_count属性添加到数据库中
  • 添加disqus onNewComment回调以更新记录上的注释计数。

使用Javascript,您可以添加回调:

 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"} }) } ]; }; 

然后你会知道未来的评论数。

文档: 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 

你不能 – 至少不是你尝试的方式。

 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" 

在将页面发送到浏览器之前,会在Rails服务器上对此进行评估。

  

此javascript稍后在客户端(用户浏览器)中运行 – 此时页面已由服务器发送。

如果您想知道何时在服务器端进行渲染,如果有任何注释需要从服务器调用Disqus API 。