我有一个Rails 3.2.1应用程序,我正在尝试在其中包含一些jquery。
以下是这些文件:
states.js:
var grid;
var columns = [
{id: "firstName", name: "FirstName", field: "FirstName"},
{id: "lastName", name: "LastName", field: "LastName"}
]
var options = {
enableCellNavigation: true,
enableColumnReorder: false
};
$(function () {
alert("hi");
var state = $("#myGrid").data("state");
$.getJSON(state, function(data){
grid = new Slick.Grid("#myGrid", data, columns, options);
});
})states_controller.rb:
class StatesController < ApplicationController
def index
@states = SiteIndexPlayer.select("DISTINCT HometownState")
end
def name
logger.debug(":name #{params[:name]}")
@athletes = SiteIndexPlayer.select('FirstName, LastName').find_all_by_HometownState(params[:name])
@state=params[:name]
respond_to do |format|
format.html
format.json { render json: @athletes }
end
end
end每次加载页面时,我都会弹出两次“嗨”。
是否有2个请求出于某种原因发出?
谢谢
问题
原来states.js被加载到DOM中两次。我没有预编译资产。为什么文件会被加载两次,还有其他的想法吗?
解决方案
ls app/assets/javascripts
application.js lib slick.core.js slick.grid.js states.js.coffee states.js 我猜states.js.coffee和application.js一样也在加载文件。一旦我删除了states.js.coffee,它就工作得很好。
知道我可以在哪里读到*.coffee吗?
编辑
application.js:
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require_tree .application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>CapApp</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>发布于 2012-02-24 01:51:24
我猜states.js.coffee和application.js一样也在加载文件。一旦我删除了states.js.coffee,它就工作得很好。
发布于 2012-07-20 08:40:38
我在Rails 3.2.x中发现了同样的症状,我在environments/staging.rb文件中进行了设置
# Expands the lines which load the assets
config.assets.debug = true然而Capistrano被配置为预编译资产,因此我在application.js中有一个副本,在特定于控制器的.coffee文件中有另一个副本。将值设置为false为我解决了这个问题。
发布于 2013-01-05 17:55:13
你有没有"rake assets:procompile"?如果是,请尝试"assets:clean“并运行服务器
https://stackoverflow.com/questions/9405301
复制相似问题