在开发中,我可以使用以下格式在页面加载时执行所有javascripts:
ready = ->
#js code here....
alert("THIS IS SOME GREAT JS CODE");
$(document).ready ready
$(document).on "page:load", ready
但是当我去推销时,当我导航到新页面时出现此错误:
Uncaught TypeError: undefined is not a function
刷新解决了让我想到js的问题。
我尝试将config.serve_static_assets
设置为true
,但这似乎没有帮助。另外,看起来Apache应该服务于静态资产?
我也尝试过bundle exec rake assets:precompile
,但也没有影响。
我还能做些什么让我的JS在页面加载时运行?
编辑:
我已将其缩小到本节(在config / environments / development.rb中):
# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = true
# Generate digests for assets URLs.
config.assets.digest = true
编辑2:
我在日志中注意到,除非完成刷新,否则服务器无法获得资产:
请求1:
I, [2015-01-23T15:56:26.996267 #16820] INFO -- : Started GET "/" for 192.168.0.10 at 2015-01-23 15:56:26 -0500
I, [2015-01-23T15:56:27.038912 #16820] INFO -- : Processing by VisitorsController#index as HTML
I, [2015-01-23T15:56:27.042287 #16820] INFO -- : Rendered layouts/_search_bar.html.slim (1.2ms)
I, [2015-01-23T15:56:27.295263 #16820] INFO -- : Rendered visitors/index.html.slim within layouts/application (254.3ms)
I, [2015-01-23T15:56:27.301084 #16820] INFO -- : Rendered layouts/_top_nav_links.html.slim (1.2ms)
I, [2015-01-23T15:56:27.302838 #16820] INFO -- : Rendered layouts/_top_nav.html.slim (4.3ms)
I, [2015-01-23T15:56:27.303462 #16820] INFO -- : Rendered layouts/_messages.html.slim (0.1ms)
I, [2015-01-23T15:56:27.304302 #16820] INFO -- : Rendered layouts/_navigation_links.html.slim (0.5ms)
I, [2015-01-23T15:56:27.304452 #16820] INFO -- : Rendered layouts/_custom_menu.html.slim (0.7ms)
I, [2015-01-23T15:56:27.305074 #16820] INFO -- : Rendered layouts/_footer.html.slim (0.4ms)
I, [2015-01-23T15:56:27.305686 #16820] INFO -- : Completed 200 OK in 266ms (Views: 54.1ms | ActiveRecord: 211.0ms)
请求2:
I, [2015-01-23T15:56:37.444562 #16820] INFO -- : Started GET "/" for 192.168.0.10 at 2015-01-23 15:56:37 -0500
I, [2015-01-23T15:56:37.485694 #16820] INFO -- : Processing by VisitorsController#index as HTML
I, [2015-01-23T15:56:37.490369 #16820] INFO -- : Rendered layouts/_search_bar.html.slim (1.4ms)
I, [2015-01-23T15:56:37.743531 #16820] INFO -- : Rendered visitors/index.html.slim within layouts/application (254.7ms)
I, [2015-01-23T15:56:37.745333 #16820] INFO -- : Rendered layouts/_top_nav_links.html.slim (0.3ms)
I, [2015-01-23T15:56:37.745334 #16820] INFO -- : Rendered layouts/_top_nav.html.slim (0.7ms)
I, [2015-01-23T15:56:37.745606 #16820] INFO -- : Rendered layouts/_messages.html.slim (0.1ms)
I, [2015-01-23T15:56:37.747027 #16820] INFO -- : Rendered layouts/_navigation_links.html.slim (0.4ms)
I, [2015-01-23T15:56:37.747204 #16820] INFO -- : Rendered layouts/_custom_menu.html.slim (0.6ms)
I, [2015-01-23T15:56:37.747876 #16820] INFO -- : Rendered layouts/_footer.html.slim (0.4ms)
I, [2015-01-23T15:56:37.748460 #16820] INFO -- : Completed 200 OK in 262ms (Views: 49.1ms | ActiveRecord: 210.8ms)
# THESE LINES HERE
I, [2015-01-23T15:56:37.987461 #16820] INFO -- : Started GET "/assets/bootstrap.css.map" for 192.168.0.10 at 2015-01-23 15:56:37 -0500
I, [2015-01-23T15:56:38.065490 #17240] INFO -- : Started GET "/assets/glyphicons-halflings-regular.woff" for 192.168.0.10 at 2015-01-23 15:56:38 -0500
I, [2015-01-23T15:56:38.066049 #16820] INFO -- : Started GET "/assets/sprites.png" for 192.168.0.10 at 2015-01-23 15:56:38 -0500