$(函数)在Rails 3.2.1应用程序中被调用两次。

时间:2022-02-18 16:02:31

I've got a Rails 3.2.1 app that I'm trying to include some jquery in.

我有一个Rails 3.2.1应用,我想在其中加入一些jquery。

Here are the files:

这是文件:

states.js:

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:

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

I'm getting the "hi" popup twice on every page load.

我在每个页面上都有两次“hi”弹出。

Are there 2 requests going out for some reason?

有两个请求出于某种原因?

Thanks

谢谢

Problem

Turns out states.js is being loaded to the DOM twice. I'm not precompiling the assets. Any other ideas why the file is getting loaded twice?

原来的状态。js被加载到DOM两次。我没有对资产进行预编译。为什么文件加载两次?

Solution

ls app/assets/javascripts
application.js  lib     slick.core.js   slick.grid.js states.js.coffee  states.js 

I'm guessing the states.js.coffee was loading the file as well as application.js. As soon as I removed states.js.coffee, it worked fine.

我猜states.js。coffee正在加载文件和application.js。一旦我移除状态。js。咖啡,它工作得很好。

Know where I might read up on *.coffee?

知道我在哪里可以读到*.咖啡吗?

Edit

application.js:

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

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>

3 个解决方案

#1


2  

I'm guessing the states.js.coffee was loading the file as well as application.js. As soon as I removed states.js.coffee, it worked fine.

我猜states.js。coffee正在加载文件和application.js。一旦我移除状态。js。咖啡,它工作得很好。

#2


1  

I found the same symptom with Rails 3.2.x, in my environments/staging.rb file I had made the setting

我发现Rails 3.2也有同样的症状。x,在我的环境/分期。rb文件我做了设置

# Expands the lines which load the assets
config.assets.debug = true

yet Capistrano was configured to precompile assets, thus I had one copy in application.js and another in the controller-specific .coffee file. Setting the value to false resolved the problem for me.

但是Capistrano被配置为预编译资产,因此我在应用程序中有一个副本。js和另一个特定于控制器的.coffee文件。将值设置为false为我解决了这个问题。

#3


0  

Did you "rake assets:procompile"? if y, please try "assets:clean" and run server

你“耙资产:procompile”吗?如果y,请尝试“资产:清洁”和运行服务器。

#1


2  

I'm guessing the states.js.coffee was loading the file as well as application.js. As soon as I removed states.js.coffee, it worked fine.

我猜states.js。coffee正在加载文件和application.js。一旦我移除状态。js。咖啡,它工作得很好。

#2


1  

I found the same symptom with Rails 3.2.x, in my environments/staging.rb file I had made the setting

我发现Rails 3.2也有同样的症状。x,在我的环境/分期。rb文件我做了设置

# Expands the lines which load the assets
config.assets.debug = true

yet Capistrano was configured to precompile assets, thus I had one copy in application.js and another in the controller-specific .coffee file. Setting the value to false resolved the problem for me.

但是Capistrano被配置为预编译资产,因此我在应用程序中有一个副本。js和另一个特定于控制器的.coffee文件。将值设置为false为我解决了这个问题。

#3


0  

Did you "rake assets:procompile"? if y, please try "assets:clean" and run server

你“耙资产:procompile”吗?如果y,请尝试“资产:清洁”和运行服务器。