如何将嵌入的红宝石代码正确地放入JavaScript? [英] How do I properly put embedded ruby code in to JavaScript?

查看:112
本文介绍了如何将嵌入的红宝石代码正确地放入JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Ruby代码的输出嵌入到JavaScript中:

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"} 

上面的行是EXACT PAGE SOURCE;它在本地运行,所以我无法向您显示该页面.

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;

通过输入:

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

我每次都会得到一个undefined值.

I get a undefined value every time.

我尝试对JavaScript中的值进行硬编码,这样就可以很好地工作,当将嵌入式Ruby放入JavaScript变量中时,它总是为我提供undefined值.

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.

推荐答案

您不应该在ERB代码周围加上引号,否则它将结果放入字符串中,而不是将其作为哈希/JSON分配给stuff目的.删除它们,它应该可以工作. (另外,<%应该是<%=).

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/') %>;

这篇关于如何将嵌入的红宝石代码正确地放入JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆