How to add to a javascript var array using content_for in Ruby on Rails? -


Right now I'm calling content_ in my code ...

& lt ; % Content_for: JavaScript -%> & Lt;% = "var boxplot_data =# {@survey.boxplot_answers.to_json};" & Gt%; & Lt;% end -% & gt; Instead, once I would like to convert an array one time, but I would add Array to the boxple_data and then have it displayed as a var. This way I can make it easier to read my code because I do not see it near to my partial data usage where I prepare it to add to the scene.

I think the best approach would be to define a helper:

  def add_to_boxplot (val) @boxplot || = [] @boxplot & lt; & Lt; Val & Def json_boxplot "var boxplot_data = # {@@spaceplot.to_json}" end  

Then just use your add_to_boxplot and instead of Yield (javascript) usage json_boxplot .


Comments