# Ruby Array to Java Script Array

**URL:** <https://discuss.rubyonrails.org/t/ruby-array-to-java-script-array/48646>\
**Category:** rubyonrails-talk\
**Created:** [January 18, 2010, 6:30am UTC](https://discuss.rubyonrails.org/t/ruby-array-to-java-script-array/48646 "2010-01-18T06:30:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prashant](https://avatars.discourse-cdn.com/v4/letter/p/51bf81/32.png) [@Prashant](https://discuss.rubyonrails.org/u/Prashant)\
**Post date:** [January 18, 2010, 6:30am UTC](https://discuss.rubyonrails.org/t/ruby-array-to-java-script-array/48646/1 "2010-01-18T06:30:31Z")

</div>

Hi,

How can we use an array created in Ruby in Java script ?

Regards, Prashant

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [January 18, 2010, 8:50am UTC](https://discuss.rubyonrails.org/t/ruby-array-to-java-script-array/48646/2 "2010-01-18T08:50:20Z")

</div>

> Hi,
> 
> How can we use an array created in Ruby in Java script ?

Convert it to appropriate strings and include it in the script. Remember that the view template is run in the server generating text that is passed to the browser. Then the javascript is run in the browser.

Colin

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [January 18, 2010, 10:12am UTC](https://discuss.rubyonrails.org/t/ruby-array-to-java-script-array/48646/3 "2010-01-18T10:12:18Z")

</div>

Colin Law wrote:

> > Hi,
> > 
> > How can we use an array created in Ruby in Java script ?
> 
> Convert it to appropriate strings and include it in the script.

You can retrieve the appropriate string by simply saying:

&nbsp;&nbsp;array.to\_json

Consider using something like

&nbsp;&nbsp;\<%= javascript\_tag( yield :js\_declarations ) %\>

in your application.html.erb and

&nbsp;&nbsp;\<% content\_for :js\_declarations, "var array = #{@array.to\_json};" %\>

in the view.

Regards, T.

---

<div class="post-metadata">

**Author:** ![Aleksey\_Gureiev](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@Aleksey\_Gureiev](https://discuss.rubyonrails.org/u/Aleksey_Gureiev)\
**Post date:** [January 18, 2010, 10:12am UTC](https://discuss.rubyonrails.org/t/ruby-array-to-java-script-array/48646/4 "2010-01-18T10:12:19Z")

</div>

Use array.to\_json and send it over to the client. For example, you could use an AJAX call to do that.

- Aleksey

---

<div class="post-metadata">

**Author:** ![Prashant](https://avatars.discourse-cdn.com/v4/letter/p/51bf81/32.png) [@Prashant](https://discuss.rubyonrails.org/u/Prashant)\
**Post date:** [January 18, 2010, 11:42am UTC](https://discuss.rubyonrails.org/t/ruby-array-to-java-script-array/48646/5 "2010-01-18T11:42:42Z")

</div>

I tried using json but I am doing some mistake because of which I am not able to get value properly I have such code in ruby file &nbsp;&nbsp;&nbsp;&nbsp;@files = Dir.glob("\*.txt").to\_json In view file &nbsp;&nbsp;var files= ('\<%= h @files -%\>'); When I see the alert I get a complete string containing list of all files When I run this alert I see one character printed at a time iso a single file name. &nbsp;&nbsp;for(i=0;i\<3;i++) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert(files[i]); &nbsp;&nbsp;&nbsp;&nbsp;}

I am making some mistake in using json but could not figure out what. Can you please help ?

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [January 18, 2010, 12:02pm UTC](https://discuss.rubyonrails.org/t/ruby-array-to-java-script-array/48646/6 "2010-01-18T12:02:24Z")

</div>

Have a look at the html source of the page (View/Page Source or similar in browser) and see what you generating. Then you should be able to work out what is wrong.

Colin

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [January 18, 2010, 12:20pm UTC](https://discuss.rubyonrails.org/t/ruby-array-to-java-script-array/48646/7 "2010-01-18T12:20:24Z")

</div>

Colin Law wrote:

> > ```
> > alert\(files\[i\]\);
> > \}
> > 
> > ```
> > 
> > I am making some mistake in using json but could not figure out what.
> 
> Have a look at the html source of the page (View/Page Source or similar in browser) and see what you generating. Then you should be able to work out what is wrong.

For this you should of course know that JS doesn't only not need HTML-escaping but doesn't understand it. So

&nbsp;&nbsp;var files = [&quot;bla&quot;,&quot;blubb&quot;];

is syntactically wrong. Should be:

&nbsp;&nbsp;var files = ["bla","blubb"];

This means: never use h (\<%h %\>) for Javascript.

T.

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [January 18, 2010, 2:34pm UTC](https://discuss.rubyonrails.org/t/ruby-array-to-java-script-array/48646/8 "2010-01-18T14:34:58Z")

</div>

T. N. T. wrote:

> Colin Law wrote:
> 
> > > Hi,
> > > 
> > > How can we use an array created in Ruby in Java script ?
> > 
> > Convert it to appropriate strings and include it in the script.
> 
> You can retrieve the appropriate string by simply saying:
> 
> &nbsp;&nbsp;array.to\_json
> 
> Consider using something like
> 
> &nbsp;&nbsp;\<%= javascript\_tag( yield :js\_declarations ) %\>
> 
> in your application.html.erb and
> 
> &nbsp;&nbsp;\<% content\_for :js\_declarations, "var array = #{@array.to\_json};" %\>
> 
> in the view.

That's kind of a bad way of doing it. Instead, put the JSON in a hidden div in your HTML, then have your JS read it from there. This will avoid mixing JS and HTML.

> Regards, T.

Best,
