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.
-
is X_Axis available to the DOM @ the time you call your function ?Brandt Solovij– Brandt Solovij2012年05月09日 22:17:26 +00:00Commented May 9, 2012 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 transferingZippie– Zippie2012年05月09日 22:20:53 +00:00Commented May 9, 2012 at 22:20
1 Answer 1
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/') %>;
8 Comments
<%= 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.<%= 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?{ "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.<%= worked. THANKS A MILLION :)Explore related questions
See similar questions with these tags.