Passing rails data to javascript

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

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

this works for me in rhtml files:


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

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 %>;

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

What’s in @user?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs