# Easy RoR \<-\> Javascript syntax question

**URL:** <https://www.ruby-forum.com/t/easy-ror-javascript-syntax-question/160186>\
**Category:** Rails\
**Created:** [February 12, 2009, 6:13pm UTC](https://www.ruby-forum.com/t/easy-ror-javascript-syntax-question/160186 "2009-02-12T18:13:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [February 12, 2009, 6:13pm UTC](https://www.ruby-forum.com/t/easy-ror-javascript-syntax-question/160186/1 "2009-02-12T18:13: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?

See the 3rd line:

========================================

========================================

Thanks a lot for helping me!

Tom

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [February 12, 2009, 10:44pm UTC](https://www.ruby-forum.com/t/easy-ror-javascript-syntax-question/160186/2 "2009-02-12T22:44:47Z")

</div>

\<%= @cities %\>

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [February 12, 2009, 11:00pm UTC](https://www.ruby-forum.com/t/easy-ror-javascript-syntax-question/160186/3 "2009-02-12T23:00:25Z")

</div>

Holy sh…

Thanks! 🙂

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [February 13, 2009, 5:17pm UTC](https://www.ruby-forum.com/t/easy-ror-javascript-syntax-question/160186/4 "2009-02-13T17:17:29Z")

</div>

On Feb 12, 12:13 pm, Tom Ha [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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?  
> […]

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—

\<%= @cities %\>
---JavaScript--- $(document).ready(function(){ var data = $("#cities").innerHTML().doWhateverYouNeedToParseIt() $("#city").autocomplete(data); });

Good luck!

## Best,

Marnen Laibow-Koser  
removed\_email\_address@domain.invalid  
[http://www.marnen.org](http://www.marnen.org)

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [February 13, 2009, 5:53pm UTC](https://www.ruby-forum.com/t/easy-ror-javascript-syntax-question/160186/5 "2009-02-13T17:53:31Z")

</div>

Thanks for your comment !  
Tom
