Est-il possible d’actualiser les partiels fréquemment en utilisant Ajax?

En arrière-plan, je veux qu’il se recharge et affiche le nombre de messages non lus qu’il contient.
Je le veux sans rafraîchir la page. Je veux dire en utilisant ajax.

Si je l’avais dans le menu, comment puis-je actualiser uniquement cette section toutes les 30 secondes?

  • <%= link_to sanitize(' ') + "received messages" + sanitize(' '+current_user.mailbox.inbox(:read => false).count(:id, :distinct => true).to_s+''), messages_received_path %>
  • messages_controller.rb

      def received if params[:search] @messages = current_user.mailbox.inbox.search_messages(@search).page(params[:page]).per(10) else @messages = current_user.mailbox.inbox.page(params[:page]).per(10) end add_crumb 'Messages Received', messages_received_path @box = 'inbox' render :index end 

    MISE À JOUR: _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _

    assets / javascript / refresh_messages_count.js

     $(document).ready(function () { // will call refreshPartial every 3 seconds setInterval(refreshPartial, 3000) }); function refreshPartial() { $.ajax({ url: "messages/refresh_part"; }) } 

    messages_controller.rb

     def refresh_part @message_count = current_user.mailbox.inbox(:read => false).count(:id, :distinct => true) # get whatever data you need to a variable named @data respond_to do |format| format.js {render :action=>"refresh_part.js"} end end 

    views / layouts / _menu.html.erb

      "layouts/message_received_count" %> 

    views / layouts / _message_received_count.html.erb

      false).count(:id, :distinct => true) > 0 %> 
  • <%= link_to sanitize(' ') + "Received" + sanitize(' '+@message_count.to_s+''), messages_received_path %>
  • <%= link_to sanitize(' ') + "Received", messages_received_path %>
  • views / messages / refresh_part.js.erb

     $('#message_received_count').html("#{escape_javascript(render 'layouts/messages_received_count', data: @message_count)}"); 

    Vous utiliserez setInterval pour envoyer la demande ajax:

     $(document).ready(function () { // will call refreshPartial every 3 seconds setInterval(refreshPartial, 3000) }); // calls action refreshing the partial function refreshPartial() { $.ajax({ url: "whatever_controller/refresh_part" }) } 

    Ensuite, vous effectuez une action dans un contrôleur comme ceci:

     def refresh_part # get whatever data you need to a variable named @data respond_to do |format| format.js end end 

    alors vous écrirez un fichier js nommé refresh_part.js.haml (vous pouvez erb au lieu de haml).

    refresh_part.js.haml ressemblerait à ceci:

     $('#part_you_want_to_refresh').html("#{escape_javascript(render 'name_of_partial', data: @data)}"); 

    Assurez-vous de définir les itinéraires corrects dans routes.rb .

    Pour info, refresh_part.js.erb ressemblerait à ceci:

     $("#part").html("<%= escape_javascript(render 'partial', data: @data) %>"); 

    au lieu de:

     $('#part').html("#{escape_javascript(render 'partial', data: @data)}"); 

    aussi, nous pouvons utiliser l’alias de “escape_javascript” pour simplifier ceci:

     $("#part").html("<%= j(render 'partial', data: @data) %>"); 

    Oui, vous pouvez

          

    This page will refresh every 5 seconds. This is because we're using the 'onload' event to call our function. We are passing in the value '5000', which equals 5 seconds.

    But hey, try not to annoy your users too much with unnecessary page refreshes every few seconds!

    Source: http://www.quackit.com/javascript/javascript_refresh_page.cfm