0
私はインデックスページに時計を表示する必要があるアプリケーションがあります。 だから私はこのような静的なコントローラがあります。Rails clock display
コントローラ/ 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
を、私はビュー/静的/ index.html.erb上でこれを持っている:
<div class="time-container">
<%= render partial: "date" %>
</div>
とこの部分ビュー/ static/_date.html.erbには、var:
<%[email protected]%>
これは、資産/ JavaScriptの/ static.jsに時刻を更新しようとする機能を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
ページのロード時、クロックが現れ、それは常に同じであり、更新されません。何か不足していますか?
と交換してください:
は取り出しますか? –
はい:GET http:// localhost:3000/static/get_time 404(見つからない) –
ルート設定がありませんか? –