33

I have a Ruby array with account ids.

I would like to store the Ruby array of account ids in a Javascript array.

I was wondering the best way to do this?

In addition, when I've been trying to do this it seems that Javascript think if there is only one account id entered then that should be the size of the array. Is there a way around this? I've tried putting it in quotes but that does not seem to work.

1
  • 1
    Using erb? using Rails? tag it accordingly. Commented Dec 4, 2011 at 12:19

4 Answers 4

46

Let's assume you are using erb. A first approach:

<%= javascript_tag "account_ids = #{account_ids.to_json.html_safe};" %>

The problem is that this creates a global variable without context (who uses it?). That's why I'd rather call a function defined somewhere in your JS code:

<%= javascript_tag "setAccounts(#{account_ids.to_json.html_safe});" %>
Sign up to request clarification or add additional context in comments.

4 Comments

Cool, that seemed to work. Any work around for when there's only when item in an array? For example, if I have one account id, say two, in the array and I call .length it thinks there is two items in the array.
@Brian: I don't understand, can you paste some code in the question? if you have a Ruby array this should just work, no matter how many elements it has (i.e account_ids.length should return 1 if it's value is [2])
Never mind. I was using a loop and I started at zero instead of one. Thanks for the help!
When the array is a value in a js object, e.g var obj = { key1: <%= @array.to_json %> }; I get: SyntaxError: illegal character key1: [\&quot;dog\&quot;,\&quot;cat\&quot;], with an arrow pointing to the first slash. But raw() works: var obj = { key1: <%= raw @array %> };
39

Here is the way works for me. Say I have array in controller:

@my_array = [['city', 'number'], ['nyc', 39], ['queens', 98]]

I want to use it in slim and generate a Google pie chart, Then I can get that JavaScript array in slim by:

javascript:
  var myJsArray = #{raw @my_array};

or I can get that JavaScript array in erb like:

var myJsArray = <%=raw @my_array%>;

Somehow, works for me.

3 Comments

The raw(ruby array) works fine for me, transforming array Ruby to Array Javascript
raw() works for me and to_json() does not when I do: var obj = {key1: <% raw @array %> };
This "raw" trick works - I did not need a json object - just my array in a JS variable. When a rendered js.erb fails in Rails, you get no error msgs to help, and you cannot test this sort of thing in the browser-inspector-console, either. Who knows how many more hours gone on this trivial task, if not for this answer. Many Thanks
5

If in your controller you have:

@my_array = [1, 2, 3]

You can set a javascript variable like this in your view:

<script type="text/javascript">
  var myJSArray = new Array(<%= @my_array.map(&:to_s).join(", ") %>);
</script>

or:

var myJSArray = [<%= @my_array.map(&:to_s).join(", ") %>];

Both cases change your ruby array of numerical id values into an array of strings and then join those string values together with commas to output valid values for javascript.

If you want string representations in your javascript you'll need to add double quotes around the values in your ruby array:

var myJSArray = [<%= @my_array.map { |some_id| '"' + some_id.to_s + '"' }.join(", ") %>];

Comments

0

based on the last solution proposed by @Shadwell, i prefer something like:

 var myJSArray = new Array("#{@my_array.map(&:inspect).join(", ")}");

my example doesn't use ERB, because it was used on a MapReduce function for mongodb.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.