【发布时间】:2017-09-02 07:43:51
【问题描述】:
我有一个需要在索引页面上显示时钟的应用程序。 所以我有一个像这样的静态控制器:
控制器/static_controller.rb:
class StaticController < ApplicationController
def index
@time = Time.now.strftime("%H:%M:%S ")
end
def get_time
@time = Time.now.strftime("%H:%M:%S ")
render partial: "date"
end
end
我在views/static/index.html.erb上有这个:
<div class="time-container">
<%= render partial: "date" %>
</div>
这个部分视图/static/_date.html.erb 只包含 var:
<%=@time%>
我使用这个 JS 函数来尝试更新 assets/javascripts/static.js 上的时间:
$(document).ready(function () {
setInterval(function () {
$('.time-container').load('/static/get_time');
}, 1000);
});
我的 routes.rb 是这样的:
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root "static#index"
resources :static
end
这样,页面加载时会显示时钟,但它始终保持不变并且不会更新。我错过了什么吗?
【问题讨论】:
-
浏览器上的 javascript 控制台是否显示任何错误?
-
是:GET localhost:3000/static/get_time 404(未找到)
-
我缺少路由配置吗?
-
yeap.. 发布您的路线文件,看看会发生什么
标签: jquery ruby-on-rails setinterval clock