Hello from a Ruby on Rails beginner!
I''ve been trying to pass some data to a variable in Javascript by using
json. Seems like the Javascript variable doesn''t receive the data
correctly.
This is the code in controller. @courses variable find the data from
database.
class CoursesController < ApplicationController
def index
@courses = Course.find(:all)
end
end
This is Javascript code in view.
<script type="text/javascript">
var markers=<%= @courses.to_json %>;
</script>
The following code show the data in browser with no problem. That means
<p><%= @courses.to_json %></p>
But the following code doesn''t show data in browser right. The output
is
a string of "undefined".
<script type="text/javascript">
var markers = <%= @courses.to_json %>;
for(i=0; i<markers.length; i++) {
document.write(markers[i].latitude, markers[i].longitude,
markers[i].name);
}
</script>
I don''t know what goes wrong. Please help. Thank you in advance.
--
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
-~----------~----~----~----~------~----~------~--~---