javascript 在 Rails 应用程序中重新加载部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10539143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reloading partial in an rails app
提问by Michael Stark
I want to reload a partial every 3 seconds on a 'new'-view in my rails app.
我想每 3 秒在我的 Rails 应用程序的“新”视图上重新加载一个部分。
I have this in my new.html.erb
我有这个 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?
我如何每 3 秒重新加载一次这个部分?我必须为此使用不显眼的 javascript 吗?如何通过 ujs 实现这一目标?
回答by Kashiftufail
Put partial in div
将部分放在 div 中
<div class="dynamic"><%= render partial: 'dynamic' %></div>
You can do it by using jquery like this
你可以通过使用 jquery 来做到这一点
$(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.........
它肯定会工作......