I'm trying to use some View instance variables inside Javascript.
My JS file is included in the head tag of my HTML. And it's a *.js file in assets/javascript.
When I try to...
console.log(<%= @some_instance_variable %>)
... I get syntax error. And if I try to...
console.log('<%= @some_instance_variable %>')
... the output is just the string <%= @some_instance_variable %>. How can I do to obtain the instance variable's actual value?. I'd really appreciate any help!.
Regards!.
asked Oct 4, 2014 at 14:33
Luis Crespo
1,6301 gold badge20 silver badges33 bronze badges
1 Answer 1
You can include it with:
<%= javascript_include_tag('file_name.js.erb'.sub('.erb', '')) %>
But in fact it's a very ugly solution. If you want to use rails variables in JS scripts, use gon gem.
answered Oct 4, 2014 at 14:58
Peter Tretiakov
3,4206 gold badges40 silver badges56 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js
<head>? What's the problem withrequire_tree .in application.js?