在Javascript中使用Ruby变量(在App视图中) [英] Using Ruby variable in Javascript (In App View)

查看:104
本文介绍了在Javascript中使用Ruby变量(在App视图中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有一个名为 @json 的视图可访问的ruby变量(其中包含我需要的JSON格式信息)

Currently, I have a ruby variable accessible by the view called @json (which contains information I need in JSON format)

但是,我想将其传递到脚本区域,例如

However, I want to pass this into a script area such as

<script type="text/javascript" charset="utf-8">

//Want @json to be usable here

</script>

有没有办法做到这一点?

Is there any way to do this?

推荐答案

假设你提到的脚本标签位于html erb视图中,你可以使用它:

Assuming the script tag you mentioned is in a html erb view you can just use this:

<script type="text/javascript" charset="utf-8">
var json = <%= @json || 'null' %>;
</script>

这篇关于在Javascript中使用Ruby变量(在App视图中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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