3

I am trying to embed the output of Ruby code in JavaScript:

var stuff= "<% Representative.get('http://0.0.0.0:4568/') %>";

The embedded part is working by itself showing a result like this:

{ "name":"John Johnson", "street":"Oslo West 555", "age":33, "phone":"555 1234567"} 

The line above is the EXACT PAGE SOURCE; it runs locally though, so I can't show you the page.

When in a variable, I try to send it to my application with this:

document.getElementById("X_Axis").value=stuff.name;

through the input:

<input type="text" id="X_Axis" />

I get a undefined value every time.

I tried hardcoding the value in the JavaScript and it works fine like that, put when the embedded Ruby is put into the JavaScript variable, it always gives me the undefined value.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Zippie
  • 6,018
  • 6
  • 31
  • 46
  • is X_Axis available to the DOM @ the time you call your function ? – Brandt Solovij May 09 '12 at 22:17
  • Yes, I have tried hardcodeing it in the same place as where i transfer the erb to js and it works. My money is on the qoutation marks of the embeded part while transfering it to js, but i have to find out how to remove the qoutation marks with a regex. Although, it won't work without them while transfering – Zippie May 09 '12 at 22:20

1 Answers1

4

You shouldn't be putting quotes around your ERB code, otherwise it puts the results in a string instead of assigning it to stuff as a hash/JSON object. Remove them and it should work. (Also, <% should be <%=).

var stuff = <%= Representative.get('http://0.0.0.0:4568/') %>;
Dylan Markow
  • 123,080
  • 26
  • 284
  • 201
  • I have tried it like that (and now once again) and then my code stops working. Maybe somehow to transfer it to a json object from string? – Zippie May 09 '12 at 22:22
  • Also, with the `<%=` it won't work. And I don't see why I should put it like that, i don't want it on the screen, just in the variable. – Zippie May 09 '12 at 22:24
  • 3
    Yes, but the `<%=` doesn't mean "put it on screen", it means "write it into the HTML, which is what you want so that it places it next to the `var stuff = `. Can you update your question showing the exact page source that's generated with your current code? – Dylan Markow May 09 '12 at 22:35
  • the `{ "name":"John Johnson", "street":"Oslo West 555", "age":33, "phone":"555 1234567"} ` is the exact page source. I have it locally though so I can't show you. – Zippie May 09 '12 at 22:40
  • So if you right-click and do View Source on your page, you only see the JSON results, you don't see `var stuff = ` anywhere?? – Dylan Markow May 09 '12 at 22:42
  • Yeah, the `var stuff =` is in the html tags `` of my application. – Zippie May 09 '12 at 22:44
  • Maybe it is confusing but... i have my application on one server and writting it in Ruby w/ Sinatra and on another server that JSON result. So I am picking up the json file with the first app with the httparty protocol and trying to show it in the browser. Hope I kind of cleared that out – Zippie May 09 '12 at 22:47
  • 2
    Sorry Dylan! I just got what you ment, you were right the first time, removing the qoutations and putting the `<%=` worked. THANKS A MILLION :) – Zippie May 09 '12 at 22:55