Ruby on rails 是否可以使用Ajax频繁地刷新部分内容?
在后台,我希望它重新加载并显示有多少未读邮件。Ruby on rails 是否可以使用Ajax频繁地刷新部分内容?,ruby-on-rails,ruby-on-rails-3,jquery,Ruby On Rails,Ruby On Rails 3,Jquery,在后台,我希望它重新加载并显示有多少未读邮件。 我想不刷新页面。我的意思是使用ajax 如果我在菜单中有这个,我怎么能每30秒刷新一次这个部分 <li><%= link_to sanitize('<i class="icon-envelope"></i> ') + "received messages" + sanitize(' <span class="badge badge-info">'+current_user.mailbox.inb
我想不刷新页面。我的意思是使用ajax 如果我在菜单中有这个,我怎么能每30秒刷新一次这个部分
<li><%= link_to sanitize('<i class="icon-envelope"></i> ') + "received messages" + sanitize(' <span class="badge badge-info">'+current_user.mailbox.inbox(:read => false).count(:id, :distinct => true).to_s+'</span>'), messages_received_path %></li>
更新:\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
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";
})
}
消息\u 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
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
视图/布局/_menu.html.erb
<span id="message_received_count"><%= render :partial => "layouts/message_received_count" %></span>
是的,你可以
<html>
<head>
<script type="text/JavaScript">
<!--
function timedRefresh(timeoutPeriod) {
setTimeout("location.reload(true);",timeoutPeriod);
}
// -->
</script>
</head>
<body onload="JavaScript:timedRefresh(5000);">
<p>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.</p>
<p>But hey, try not to annoy your users too much with unnecessary page refreshes every few seconds!</p>
</body>
</html>
此页面将每5秒刷新一次。这是因为我们正在使用“onload”事件来调用我们的函数。我们正在传递值“5000”,它等于5秒
但是,嘿,不要每隔几秒钟就用不必要的页面刷新来打扰你的用户
来源:您将使用setInterval发送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"
})
}
然后在控制器中执行如下操作:
def refresh_part
# get whatever data you need to a variable named @data
respond_to do |format|
format.js
end
end
$('#part_you_want_to_refresh').html("#{escape_javascript(render 'name_of_partial', data: @data)}");
$("#part").html("<%= escape_javascript(render 'partial', data: @data) %>");
然后您将编写一个名为refresh\u part.js.haml
(您可以使用erb代替haml)的js文件
refresh_part.js.haml将如下所示:
def refresh_part
# get whatever data you need to a variable named @data
respond_to do |format|
format.js
end
end
$('#part_you_want_to_refresh').html("#{escape_javascript(render 'name_of_partial', data: @data)}");
$("#part").html("<%= escape_javascript(render 'partial', data: @data) %>");
确保在
路由中设置了正确的路由。rb
仅供参考,refresh\u part.js.erb如下所示:
def refresh_part
# get whatever data you need to a variable named @data
respond_to do |format|
format.js
end
end
$('#part_you_want_to_refresh').html("#{escape_javascript(render 'name_of_partial', data: @data)}");
$("#part").html("<%= escape_javascript(render 'partial', data: @data) %>");
此外,我们还可以使用别名“escape_javascript”来简化此过程:
$("#part").html("<%= j(render 'partial', data: @data) %>");
$(“#部分”).html(“”);
谢谢您的建议。但是我想使用rails来实现它,只应用部分:)看看这个[问题],我想这就是你需要的。[1][1]:谢谢,我应该把ajax请求部分放在哪里???我应该直接写给我的控制器吗?或者我需要创建新文件或其他东西吗???jquery部分应该在视图中包含的js文件中,而操作应该在控制器中。是的,我知道。但是我想知道为什么要将ajax请求和刷新部分js分开???你能给我一个例子,把ajax请求放在哪里吗?refresh_part.js.haml是一个文件,服务器将作为对ajax请求的响应提供该文件,它不会包含在视图的js中。检查一下这个不引人注目的Javascript,这个教程啊,我明白你的意思了!所以我必须把javascript的第一块放在layouts/application.html.erb的头部?正确的??如果是这样,我是否应该创建文件并在application.html.erb中呈现该文件?