2017-04-12 69 views
0

我有這樣的幫手鍊接如何顯示的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中

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

和所有工作正常我得到一個數字,數量是在我的索引視圖中每個職位的disqus評論計數器,但如何顯示數字如果是否大於0?如果等於0我不想在視圖中顯示它。有人知道如何解決這個問題?非常感謝

我試着用這樣的:

此列添加到產品COMMENT_COUNT:整數

我改變了我的_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  

來源: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) 

不允許的參數:增量,ID

控制檯

COMMENT_COUNT:無

有人能幫助我嗎?

+0

你如何恢復disqus評論櫃檯?你有模型方法嗎? –

+0

我在applicationacion.layout%script {id:「dsq-count-scr」,src:「https://urlyoururl.disqus.com/count.js」,async:「async」}中獲得了這個腳本 –

+0

我不認識那個代碼,請提供該腳本的更多細節(關於這個問題)。 –

回答

1

我能想到的解決這一點的最好辦法是:

  • 一個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 
+0

我試着用你的信息兄弟,但沒有工作,我已經更新了我的問題,我必須寫什麼路線? –

+0

您應該運行'rake routes'並找到更新產品的路線。可能''products_path'具有'patch'方法。 –

+0

兄弟,我沒有告訴過你,但我得到了401完成401未授權391ms(ActiveRecord:0.0ms)。我已經更新了我的問題 –

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" 

在頁面發送到瀏覽器之前,在Rails服務器上對此進行評估。

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

這個JavaScript是在客戶端(用戶的瀏覽器)後運行 - 在這一點上的頁面由服務器已經發出。

如果您想知道在服務器端渲染時是否有任何註釋,您需要從服務器撥打Disqus API

+0

好,但我該如何解決它?我不知道該怎麼做:/ –

+0

它叫做閱讀... – max