Reloading partial in an rails app

折月煮酒 提交于 2019-11-29 09:10:47

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/10539143/reloading-partial-in-an-rails-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!