Hi, How can we use an array created in Ruby in Java script ? Regards, Prashant -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
2010/1/18 Prashant <prashant.thakurs-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> 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> > Regards, > Prashant > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > > > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law wrote:> 2010/1/18 Prashant <prashant.thakurs-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: >> 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: array.to_json Consider using something like <%= javascript_tag( yield :js_declarations ) %> in your application.html.erb and <% content_for :js_declarations, "var array = #{@array.to_json};" %> in the view. Regards, T. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Use array.to_json and send it over to the client. For example, you could use an AJAX call to do that. - Aleksey On Jan 18, 5:30 pm, Prashant <prashant.thak...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > How can we use an array created in Ruby in Java script ? > > Regards, > Prashant-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 @files = Dir.glob("*.txt").to_json In view file 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. for(i=0;i<3;i++) { alert(files[i]); } I am making some mistake in using json but could not figure out what. Can you please help ? On Jan 18, 3:12 pm, "T. N.t." <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Colin Law wrote: > > 2010/1/18 Prashant <prashant.thak...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > >> 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: > > array.to_json > > Consider using something like > > <%= javascript_tag( yield :js_declarations ) %> > > in your application.html.erb and > > <% content_for :js_declarations, "var array = #...@array.to_json};" %> > > in the view. > > Regards, T. > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
2010/1/18 Prashant <prashant.thakurs-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> 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 > @files = Dir.glob("*.txt").to_json > In view file > 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. > for(i=0;i<3;i++) > { > 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. Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law wrote:> 2010/1/18 Prashant <prashant.thakurs-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: >> 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 var files = ["bla","blubb"]; is syntactically wrong. Should be: var files = ["bla","blubb"]; This means: never use h (<%h %>) for Javascript. T. -- Posted via http://www.ruby-forum.com/. --0015174c0d5ccca8bc047d6f6004 Content-Type: text/plain; charset=ISO-8859-1 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. --0015174c0d5ccca8bc047d6f6004--
T. N. T. wrote:> Colin Law wrote: >> 2010/1/18 Prashant <prashant.thakurs-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: >>> 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: > > array.to_json > > Consider using something like > > <%= javascript_tag( yield :js_declarations ) %> > > in your application.html.erb and > > <% 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, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. --0022158df84f3d1bde047d71424c Content-Type: text/plain; charset=ISO-8859-1 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. --0022158df84f3d1bde047d71424c--
Apparently Analagous Threads
- How to access controller's instance variable in a js.erb loaded by javascript_include_tag?
- Is there a way I can create a sub-layout with ERB?
- `stack level too deep` when overwriting `to_json`
- additional model attributes
- serializable_hash and serializable_add_includes