# Easy RoR \<-\> Javascript syntax question

**URL:** <https://discuss.rubyonrails.org/t/easy-ror-javascript-syntax-question/39340>\
**Category:** rubyonrails-talk\
**Created:** [February 13, 2009, 4:16pm UTC](https://discuss.rubyonrails.org/t/easy-ror-javascript-syntax-question/39340 "2009-02-13T16:16:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marnen\_Laibow-Koser](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@Marnen\_Laibow-Koser](https://discuss.rubyonrails.org/u/Marnen_Laibow-Koser)\
**Post date:** [February 13, 2009, 4:16pm UTC](https://discuss.rubyonrails.org/t/easy-ror-javascript-syntax-question/39340/1 "2009-02-13T16:16:08Z")

</div>

> Hi there,
> 
> a simple n00b question:
> 
> I have that Javascript snippet on my page (see below; taken from jquery, actually).
> 
> I want to pass it the "@cities" array from the controller. But what's the correct syntax, here?

[...]

> \<script\> $(document).ready(function(){ var data = @cities; \<---- that's not correct, is it? $("#city").autocomplete(data); }); \</script\>

Small note: you really should not be using inline JavaScript (see [Unobtrusive JavaScript - Wikipedia](http://en.wikipedia.org/wiki/Unobtrusive_JavaScript) for more information). So while Freddy's solution will work, I would suggest putting your JavaScript in a separate file. In this case, there are two ways to get the value of @cities into the JavaScript code:

1. Use a .js.erb file and embed the value in ERb, just as you would in an inline script. This is very simple, but has the disadvantage that the browser cannot cache the script file, since the code is dynamically generated every time. 2 (my usual method). ---stylesheet--- .hidden: {display: none;} ---view--- \<div id="cities" class="hidden"\>\<%= @cities %\>\</div\> ---JavaScript--- $(document).ready(function(){ &nbsp;&nbsp;var data = $("#cities").innerHTML().doWhateverYouNeedToParseIt() &nbsp;&nbsp;$("#city").autocomplete(data); });

Good luck!

Best,
