I’ve got a Rails 3.2.1 app that I’m trying to include some jquery in.
Here are the files:
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
I’m getting the "hi" popup twice on every page load.
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?
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.
Know where I might read up on *.coffee?
Edit
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>
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.