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?

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