Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a variable in my controller @data that is a very long string. I have a javascript function that needs access to this variable. Should I do this:

$(document).ready(function(){
  var data='<%=j @data%>'
})

or should I put the string in an html data-attribute and then do something like:

<div data-stuff="<%=h @data%>"></div>

$(document).ready(function(){
  var data=$("div").data('stuff')
})

If one is preferable, why?

share|improve this question

closed as primarily opinion-based by Arun P Johny, trudyscousin, meagar, Chris, andand Feb 18 '14 at 5:57

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

    
if the value is not related to an element then don't use it as attribute, use it as a variable as you have done in the first sample –  Arun P Johny Feb 18 '14 at 4:10
    
It isn't it is just unrelated text. But I was told that the first example would be slower because rails would have to render the javascript each page load and couldn't cache it. But in the second it still has to render the html each time...should it really make a difference? –  kempchee Feb 18 '14 at 4:15

1 Answer 1

up vote 1 down vote accepted

Between your two options, the former is preferred.

But consider using the gon gem: https://github.com/gazay/gon

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.