最近我发布了这个问题,所以我尝试了另一种方法来缓存javascript和css,比如
这是routes.rb
offline = Rack::Offline.configure do
cache ActionController::Base.helpers.asset_path("application.css")
cache ActionController::Base.helpers.asset_path("application.js")
cache ActionController::Base.helpers.asset_path("jquery.offline.js")
cache ActionController::Base.helpers.asset_path("jquery.tmpl.min.js")
cache ActionController::Base.helpers.asset_path("json.js")
network "/"
end
get "/application.manifest" => offline
这是application.html.erb
<!DOCTYPE html>
<html manifest="/application.manifest">
<head>
<title>OfflineTest</title>
<%= stylesheet_link_tag 'application', media: 'all' %>
<%= javascript_include_tag 'application' %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
这是index.html.erb
Listing
<%= form_for Item.new do |f| %>
<p>
<%= f.text_field :name %>
<%= f.submit "Add" %>
</p>
<% end %>
<script type="text/html" id="item_template">
<li>${name}</li>
</script>
<ol id="items">
<li><em>Loading items...</em></li>
</ol>
这是application.js
//= require jquery
//= require jquery_ujs
//= require jquery.tmpl.min
//= require jquery.offline
//= require json
//= require_tree .
$(document).ready(function(){
if ($.support.localStorage) {
$(window.applicationCache).bind("error", function() {
console.log("There was an error when loading the cache manifest.");
});
if (!localStorage["pendingItems"]) {
localStorage["pendingItems"] = JSON.stringify([]);
}
$.retrieveJSON("/items.json", function(data) {
var pendingItems = $.parseJSON(localStorage["pendingItems"]);
$("#items").html($("#item_template").tmpl(data.concat(pendingItems)));
});
} else {
alert("Try a different browser.");
}
});
并添加了宝石
gem "rack-offline"
之后,当我去申请时,它似乎是
CACHE MANIFEST
# 1ad140903542ef1549ddf41cb57e9105786a8b344d7fab9278323e9402e61d14
/assets/application.css
/assets/application.js
/assets/jquery.offline.js
/assets/jquery.tmpl.min.js
/assets/json.js
NETWORK:
/
所以问题来自这里,当我停止服务器并检查,它没有加载JS和CSS文件,这意味着没有加载视图中的数据,但它与在线工作,实际上我已经尝试过相同的方式rails 4.0.2和ruby 2.0.0有效。但是当我使用rails 4.1.6和ruby 2.1.2时,它无法使用离线模式。我的配置和所有这些都有问题吗?