Passing rails data to javascript


#1

Hi there
how do I pass rails data to a JS array?
like
arr[0] = @user.name
arr[1] = @user.location

something like that
I’ve been trying differente approaches but no luck so far
thanks in advance


#2

this works for me in rhtml files:

HTH


#3

thanks a lot guys
I tried the to_json but for a lame reasson
it broke the app
webrick wont start


#4

to_json is great for this.

var user_obj = <%= @user.to_json %>;

If you want an array of values:

var arr = <%= @user.values.to_json %>;


#5

Weird… can you try: @user.attributes.to_json?

What’s in @user?