我对javascript非常陌生,我正在尝试在rails应用程序中设置轮询,以从活动模型中提供简化的活动提要。我在这里密切关注Railscast的民意调查:http://railscasts.com/episodes/229-polling-for-changes-revised?view=asciicast
我设置了一个非常简单的测试警报,以便在加载页面时触发,但在加载页面后不会出现警报。我希望有人能澄清为什么它没有出现。
以下是视图:
<h4>Activities</h4>
<div class="well col-sm-5 activity_monitor" data-url="<%=activities_path%>">
<% @activities.each do |activity| %>
Student <%= activity.trackable_id %> began a tutoring session at <%=activity.created_at %>
<br>
<% end %>
</div>
这是我的简单咖啡脚本:
ActivityPoller =
poll: ->
setTimeout @request, 5000
request: ->
$.get($('.activity_monitor').data('url'))
jQuery ->
if $('.activity_monitor').length > 0
ActivityPoller.poll()
这是控制器:
class ActivitiesController < ApplicationController
def index
@activities=Activity.all
end
end
这是index.js.erb文件:
alert("This is a test alert");
当我加载页面时,这是我在控制台中看到的输出:
Consider using 'dppx' units, as in CSS 'dpi' means dots-per-CSS-inch, not dots-per-physical-inch, so does not correspond to the actual 'dpi' of a screen. In media query expression: only screen and (-webkit-min-device-pixel-ratio: 1.5), only screen and (min-resolution: 144dpi) activities:1
Uncaught ReferenceError: define is not defined wizard.js?body=1:10(anonymous function) wizard.js?body=1:10
GET http://localhost:8080/assets/jquery.nanoscroller.min.js.map 404 (Not Found) activities:102
'Range.detach' is now a no-op, as per DOM (http://dom.spec.whatwg.org/#dom-range-detach). bootstrap3-wysihtml5.min.js?body=1:56
The "fb-root" div has not been created, auto-creating sdk.js:56
我发现没有调用javascript警报,因为控制器操作没有明确的responsd_to javascript块。令人困惑的是,Railscast在源代码中没有这个respons_to块。它在这种情况下工作,因为没有index.html.erb文件。在我的例子中,我有一个index.html.erb文件,因此有必要指定各种responsd_to选项,以便Rails知道要渲染什么。