I have a view that will show sports teams. When you click on a team an information tab gets populated with schedule, roster, etc. via ajax, which calls the controller to handle a call to an external API or the local DB (depending on the data to show). The ajax then renders a partial with the returned data, via a coffeescript file.
So far it's all working great -- but the returned partial in the coffeescript never gets executed. You can see a jsfiddle with the exact js returned by the partial here: http://jsfiddle.net/43XAQ/1/
The coffeescript file looks like this:
$(document).ready -> // same issue with or without the document.ready
<% @poll.each do |p| %>
$ = jQuery; // tried with and without this line-- no change
$('#poll_body').append("<%= j render(partial: 'poll/place', locals: { place: p }) %>")
<% end %>
The view it's loading has a simple table, like in the fiddle above.
I thought that it was turbolinks causing it, since I'd had that problem on other js elements-- but I installed the jquery-turbolinks
gem and it resolved the other issues, but not this one. The exact same code works great in the jsfiddle but not in my local site. So I assume there is something to do with how Rails loads the js?
EDIT: yes, I used dev tools to look at the returned content. Sorry if that was unclear-- it's exactly what is pasted as the js component in the fiddle I linked above:
(function() {
var $;
$ = jQuery;
return $('#poll_body').append("<tr>\n <td>10<\/td>\n <td><a href=\"/team/236\">Oklahoma State Cowboys<\/a><\/td>\n <td>AP Poll<\/td>\n <td>3<\/td>\n <\/tr>");
});
}).call(this);
EDIT2:
A few more points:
1) if I do nothing in the coffescript file except alert or console log, it still doesn't do anything.
2) the actual call to the controller that invokes the coffeescript is a standard jQuery click event. There is a polls.js
file loaded in the poll view, which does this:
$( document ).ready(function() {
$('#poll').on('click', '.poll-item', function(){
doActive(this);
current_team = this.id;
});
});
// snip //
var doActive = function(el){
// some other stuff
getNews(current_team); // calls a webservice via ajax, works fine.
getRoster(current_team); // calls a webservice via ajax, works fine.
getPolls(current_team); // calls the controller via ajax, returns coffeescript properly, coffeescript never gets executed
}