[英]How to show a link_to helper only if it returns > 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:integer
我更改了_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>
產品控制器
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:無
有人可以幫助我嗎?
我認為可以解決此問題的最佳方法是:
comment_count
屬性 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服務器上對此進行評估。
<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 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.