Creating Javascript array from RAILS model data

Hi and thanks!

I think this should be simple: I need to initialize and then ajax
update a Javascript array; taking the data from a rails model. I wrote
an iterator that created a string–which looked just like my working
test array, but then that didn’t seem to work out for writing it to
the page.

How is this done?
Thanks,
Ed

Hi Ed,
Definitely give JSON a look if you haven’t already. That’s pretty much
what it’s made for.
In the action that your JS is calling, use this render option:

render :json => your_data.to_json

Or use that in a respond_to block.

There’s also a little bit on returning JSON here:
http://api.rubyonrails.com/classes/ActionController/Base.html

  • Doug

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