I need to get multiple complex sets of data into Javascript

时间:2018-01-11 08:38:12

标签: ruby-on-rails ruby

So I have a database that I'm trying to display reports on using the Highcharts javascript library.

The issue is i have no idea how to effectively get this data from Rails into Javascript.

Simply using html data attributes doesnt scale because i have many charts and many different sets of data for each one. I need to exclude certain columns, join tables, etc.

Any ideas?

1 个答案:

答案 0 :(得分:1)

有几种选择:

  1. 使用 gon
  2. 创建实例变量并将其传递到html模板

    https://github.com/gazay/gon

    1. 使用ActiveModelSerializers创建端点并格式化数据,例如:https://www.engineyard.com/blog/active-model-serializers

    2. 您可以创建一个 jbuilder 视图,并通过javascript ajax调用调用的端点呈现它

    3. https://github.com/rails/jbuilder

      最低功耗 Gon ,但您发送到其中的所有内容都可在HTML源代码中查看。