Easy RoR <-> Javascript syntax question

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

<%= @cities %>

Holy sh…

Thanks! :slight_smile:

On Feb 12, 12:13 pm, Tom Ha [email protected] 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 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
[email protected]
http://www.marnen.org

Thanks for your comment !
Tom