如何从JavaScript上的Rails访问环境变量? [英] How to access environment variable from rails on your javascript?

查看:82
本文介绍了如何从JavaScript上的Rails访问环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在要构建的应用程序中建立一个关键系统.我有一个具有以下逻辑的代码:

I am trying to set up a key system in the application I'm building. I have a code that has this logic:

$('.key-test-button').on('click', function(){
    if ($('.key-test-input').val() === "MYKEYHERE"){
        $('#hidden-div-instructor').show();
    } 
});

我的想法是将"MYKEYHERE"替换为我将存储为环境变量的代码上的隐藏值.我知道如何在例如Rails内部的API上执行此操作,但是我不熟悉如何在javascript/jquery脚本中访问此变量.如果有人可以给我一个见识,我会很乐意.谢谢.

And My Idea was to replace "MYKEYHERE" for a hidden value on my code that I would store as a environment variable. I know how to do that for APIs for example, inside rails, but Im not familiar how I could access this variable inside my javascript/jquery script. I would love if someone could give me a insight. Thanks you.

推荐答案

正如@davidhoelzer所述,您无法使用纯js(据我所知)...但是,您可以更改.js文件添加到.js.erb并使用ruby那样访问/操作环境变量.

As @davidhoelzer mentioned, you can't do this with pure js (as far as I know)... you could, however, change the .js file to .js.erb and use ruby to access/manipulate your environment variable that way.

这篇关于如何从JavaScript上的Rails访问环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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