Reloading partial in an rails app

12,615

Put partial in div

   <div class="dynamic"><%= render partial: 'dynamic' %></div>

You can do it by using jquery like this

       $(document).ready(
         function() {
          setInterval(function() {
            $('.dynamic').load('/controller_name/action_name');
        }, 3000);
    });

Now refresh partial in controller action for load new content

         def action_name
            render :partial => "directory_name/dynamic"
         end

It will sure work.........

Share:
12,615
Michael Stark
Author by

Michael Stark

Updated on July 16, 2022

Comments

  • Michael Stark
    Michael Stark almost 2 years

    I want to reload a partial every 3 seconds on a 'new'-view in my rails app.

    I have this in my new.html.erb

    <h1>Controller#new</h1>
    This is my static content
    <%= render partial: 'dynamic' %>
    More Static content
    

    How do I get this partial reloaded every 3 seconds? Do I have to use unobtrusive javascript for this? How can I achieve this through ujs?

  • Anton Semenichenko
    Anton Semenichenko over 5 years
    Thank you, please note this is very helpful
  • Metaphysiker
    Metaphysiker over 5 years
    How do you pass variables to the partial? This doesn't work: render partial: "courses/navigation_buttons", current_slide: (at)current_slide, course: (at)course