Reputation: 11
I'm triying to include some js.erb script into my view. Simply because I'm coding a polling based chat I want to keep it really simple but it actually ended up being something really much more complicated than what i initially thought.
Here is my js.erb script code:
$( document ).ready(function() {
$(function() {
setTimeout(updateMessages, 1000);
});
function updateMessages () {
var game_id = $("#game_chat").attr("data-id");
$("#messages").html("<%=escape_javascript(render(:partial => 'messages')) %> ");
setTimeout(updateMessages, 1000);
}
});
and my partial is simply
<%= render @messages %>
I really got stucked here and it seems like there's no easy way to include .js.erb code inside a view. I mean something like:
<%= javascript_include_tag "games/messages.js.erb" %>
Upvotes: 0
Views: 1468
Reputation: 584
Well you don't need .js.erb if you're using the asset pipeline. <%= javascript_include_tag "games/messages" %>. Tilt (the engine that decides which template engine to use) will start at the right and work its way back so the browser and view really only see the .js file. That being said, from a JS perspective doing this opens yourself up to a whole range of ugliness. When you include a large string like that it's going to trigger an eval on your code. This is fine if you can 100% trust your source, but opens yourself up to xss if you're not careful.
Upvotes: 1