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: ======================================= <script> $(document).ready(function(){ var data = @cities; <---- that''s not correct, is it? $("#city").autocomplete(data); }); </script> ======================================= Thanks a lot for helping me! Tom -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
<%= @cities %> On Feb 12, 9:13 am, Tom Ha <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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? > > See the 3rd line: > > =======================================> > <script> > $(document).ready(function(){ > var data = @cities; <---- that''s not correct, is it? > $("#city").autocomplete(data); > }); > </script> > > =======================================> > Thanks a lot for helping me! > > Tom > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Holy sh... Thanks! :-) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Feb 12, 12:13 pm, Tom Ha <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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?[...]> <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 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(){ var data = $("#cities").innerHTML().doWhateverYouNeedToParseIt() $("#city").autocomplete(data); }); Good luck! Best, -- Marnen Laibow-Koser marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org http://www.marnen.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks for your comment ! Tom -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---