"#{url_for([product, {only_path: false}])}" }, class: "no-underline bold grey-text text-darken-3 margin-left"layout: application.r" name="description" /> "#{url_for([product, {only_path: false}])}" }, class: "no-underline bold grey-text text-darken-3 margin-left"layout: application.r" />
18807

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

Question:

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 <strong>is greater than 0?</strong> if <strong>is equal to 0</strong> I dont wanna show it in the view. Someone know how to resolve this problem? Thanks a lot

i tried with this:

<blockquote>

add this column to products comment_count :integer

</blockquote>

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: <a href="https://help.disqus.com/customer/portal/articles/466258-capturing-disqus-commenting-activity-via-callbacks" rel="nofollow">https://help.disqus.com/customer/portal/articles/466258-capturing-disqus-commenting-activity-via-callbacks</a>

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

Unpermitted parameters: increment, id

</blockquote>

in console

<blockquote>

comment_count: nil

</blockquote>

someone can help me?

Answer1:

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

<ul><li>add a comment_count attribute to your database</li> <li>add a disqus onNewComment callback to update the comment count on your record.</li> </ul>

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: <a href="https://help.disqus.com/customer/portal/articles/466258-capturing-disqus-commenting-activity-via-callbacks" rel="nofollow">https://help.disqus.com/customer/portal/articles/466258-capturing-disqus-commenting-activity-via-callbacks</a>

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

Answer2:

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 <a href="https://disqus.com/api/docs/" rel="nofollow">Disqus API</a> from the server.

Recommend

  • React JS how to get script inside dangerouslySetInnerHTML executed
  • Asp.Net - Detect no javascript on page? (renamed title)
  • Google Tag Assistant asks fo feed ID
  • ThemeChanger in GWT-Ext
  • Session management in GWT client side
  • pure javascript dom dynamic insert, update and delete
  • After converting my FBX file to a .gltf, the model is incredibly small, why?
  • Creating a layer of gradient within an SVG path dynamically
  • Can a variable be stored within an image or div tag?
  • RavenDB indexing errors
  • Why does the following throw an “Object doesn't support property or method 'importNode
  • Using Nested Table variables / Collections in SQL inside PL/SQL blocks
  • Tell Git to stop prompting me for conflicts when none really exist?
  • Angularjs pass function from Controller to Directive (or call controller function from directive) -
  • Simulate click Geckofx vb,net
  • saving file generated by TCPDF
  • Eloquent update method change created_at timestamp
  • jQuery ready not fired after rails link_to is clicked
  • Alternative To body {overflow:scroll;} That Will Prevent Page Jostling/Wriggling?
  • How do I pass the string value parameter of the selected list item from an auto-populated dropdown l
  • Play WS (2.2.1): post/put large request
  • Jquery UI tool tip close icon
  • FileReader+canvas image loading problem
  • Display issues when we change from one jquery mobile page to another in firefox
  • Illegal mix of collations for operation for date/time comparison
  • HTML download movie download link
  • Updating server-side rendering client-side
  • Change an a tag attribute in JavaScript based on screen width
  • Release, debug version and Authorization Google?
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Cannot Parse HTML Data Using Android / JSOUP
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • How to set the response of a form post action to a iframe source?
  • Hits per day in Google Big Query
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • Getting Messege Twice Using IMvxMessenger
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • How to Embed XSL into XML
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • How to load view controller without button in storyboard?