# Pass json data into javascript

**URL:** <https://www.ruby-forum.com/t/pass-json-data-into-javascript/153780>\
**Category:** Rails\
**Created:** [November 21, 2008, 12:47am UTC](https://www.ruby-forum.com/t/pass-json-data-into-javascript/153780 "2008-11-21T00:47:39Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![wintersleep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e47774/32.png) [@wintersleep](https://www.ruby-forum.com/u/wintersleep)\
**Post date:** [November 21, 2008, 12:47am UTC](https://www.ruby-forum.com/t/pass-json-data-into-javascript/153780/1 "2008-11-21T00:47:39Z")

</div>

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.

The following code show the data in browser with no problem. That means

\<%= @courses.to\_json %\>

But the following code doesn’t show data in browser right. The output is  
a string of “undefined”.

I don’t know what goes wrong. Please help. Thank you in advance.
