# Getting data from Rails to Javascript

**URL:** <https://www.ruby-forum.com/t/getting-data-from-rails-to-javascript/141959>\
**Category:** Rails\
**Created:** [July 2, 2008, 2:38am UTC](https://www.ruby-forum.com/t/getting-data-from-rails-to-javascript/141959 "2008-07-02T02:38:45Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![pedz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@pedz](https://www.ruby-forum.com/u/pedz)\
**Post date:** [July 2, 2008, 2:38am UTC](https://www.ruby-forum.com/t/getting-data-from-rails-to-javascript/141959/1 "2008-07-02T02:38:45Z")

</div>

This started when I upgraded to Rails 2.1. My “roll you own” in place  
editor broke because they need the authentication token.

What I did before was I wrapped the editor in a span of a particular  
class. In the span, I add an options attribute and a url attribute.  
When the page loaded, the on load hook would find the spans of that  
class and use the options and url as part of the in place editor  
customization. This almost completely separated the html from the  
javascript.

But another related event is during some recent debugging, I passed my  
pages through the HTML validator and it doesn’t like these options and  
url attributes stuck in a span. And, in general, I need more power than  
what I have.

So, one thought I had was to have a hash, lets call it  
javascript\_objects. As the render code executed, it would add things to  
this hash. Then the last thing in the layout would be to dump this hash  
out as a JSON object in a script tag. It would be wrapped with  
something like pageOptions and would create a javascript hash (object).  
Details left to the reader.

The pre-written javascript code would look in this pageOptions object  
for the options for each of the in placed editors, or whatever other  
variable data that Rails knows but is not fixed.

I figure, like all my other ideas in this area, this isn’t a new idea.  
Has this been done? Are there other trends of solving the general issue  
of how to get Rails knowledge to the javascript code? e.g. what URL do  
you post?
