How to show a link_to helper only if it returns > 0

核能气质少年 提交于 2020-01-07 03:59:10

问题


I have this helper link

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

<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>

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

i tried with this:

add this column to products comment_count :integer

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

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

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

but i get this error

   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)

Unpermitted parameters: increment, id

in console

comment_count: nil

someone can help me?


回答1:


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

  • add a comment_count attribute to your database
  • add a disqus onNewComment callback to update the comment count on your record.

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.

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

Change your Controller to:

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



回答2:


You can't - at least not in the way you attempting.

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"

This is evaluated on your Rails server before the page is sent to the browser.

<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>

This javascript is run later in the client (the users browser) - at that point the page is already sent by the server.

If you want to know when rendering on the server side if there are any comments you need to make a call to the Disqus API from the server.



来源:https://stackoverflow.com/questions/43358469/how-to-show-a-link-to-helper-only-if-it-returns-0

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