Skip to content Skip to sidebar Skip to footer

Ruby Array To Js Array In .js.erb File On Rails

I'm trying to turn a ruby array into a js array in a js.erb file. I've checked all questions on this in stack overflow but it isn't working for me. Here is the function function

Solution 1:

I assume, you are gettings @keys data from db or local. Could you try gon gem to pass the values from ruby to js. Here is the railscast tutorial for the same: http://railscasts.com/episodes/324-passing-data-to-javascript?view=asciicast

And see, if that works.

Let me know.

Solution 2:

After converting the array to JSON, you just need to mark it as HTML safe so it will render Javascript code.

This will do the trick:

<%= @keys.to_json.html_safe %>

Post a Comment for "Ruby Array To Js Array In .js.erb File On Rails"