Brandon Fratello
2010-May-16 01:16 UTC
getting the correct index in a ruby array in a javascript lo
So I''m very new to both ruby and javascript, but the basic problem is. I have a ruby array @events_this_month, and i''m running a javascript loop that basically needs the information from this array. The problem is I can''t seem to set the proper index for my ruby array. What I have looks like this: for( var i = 0; i < 3; i++) { var title = ''<%= @events_this_month[i].title %>''; alert(title); } I''ve also tried: <%= $j = 0 %> for( var i = 0; i < 3; i++) { var title = ''<%= @events_this_month[$j].title %>''; alert(''<%= $j %>''); <%= $j = $j +1 %> } Any suggestions on how I can access the correct index in my ruby array during the javascript loop? -- 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.
Tim Lovett
2010-May-16 02:25 UTC
Re: getting the correct index in a ruby array in a javascript lo
You''re confusing server side and client side code. Javascript works after a webpage has been returned but Ruby works when a page is being fetched. So if you want to be able to iterate over a bunch of values you''ll need to create a javascript array that contains the values of the Ruby array. *If you want to convert it to a javascript array:* http://api.rubyonrails.org/classes/ActionView/Helpers/JavaScriptHelper.html#M001761 I suggest you just make an array of titles and then pass it in via an options array to your code or something to keep the view layer clean. *So in your controller set: *options[:title] Equal to an array of the titles from your object array. *Something like:* var myEvents = <%= array_or_string_for_javascript(options[:title]) %>; for( var i = 0; i < myEvents.length; i++) { alert (myEvents[i]); } Someone correct me if I''m explaining anything wrong or if there is something wrong with my syntax -- I''m beginning ruby and rails myself so I think that''s the correct syntax. Tim On Sat, May 15, 2010 at 9:16 PM, Brandon Fratello <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote:> So I''m very new to both ruby and javascript, but the basic problem is. I > have a ruby array @events_this_month, and i''m running a javascript loop > that basically needs the information from this array. The problem is I > can''t seem to set the proper index for my ruby array. What I have looks > like this: > > > for( var i = 0; i < 3; i++) > { > var title = ''<%= @events_this_month[i].title %>''; > alert(title); > } > > I''ve also tried: > > <%= $j = 0 %> > for( var i = 0; i < 3; i++) > { > var title = ''<%= @events_this_month[$j].title %>''; > alert(''<%= $j %>''); > <%= $j = $j +1 %> > } > > Any suggestions on how I can access the correct index in my ruby array > during the javascript loop? > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@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.