简体   繁体   English

为什么我的ruby代码不插入我的javascript文件?

[英]Why doesn't my ruby code interpolate with my javascript files?

I am playing around with ajax to render a highchart graph on a web page. 我正在玩ajax在网页上渲染高图图形。 What I'd like to do is to populate the fields on highchart with ruby information from my controller but so far I'm unable to make this work. 我想做的是用来自控制器的红宝石信息填充highchart上的字段,但到目前为止我还无法完成这项工作。 I can't seem to implement what Ryan Bates is doing in this episode of Railscasts Railscasts with charts 我似乎无法实现Ryan Bates在本集的Railscasts中使用图表进行的操作

I begin the rendering in this html.haml file: 我开始在此html.haml文件中进行渲染:

= link_to(t('compare.view_as_graph'), compare_graph_projects_url(project_data: opts[:graph]), remote: true)
.modal.fade#graphModal

Then this takes me to the compare_controller where this code is executed: 然后,这将我带到执行以下代码的compare_controller:

def projects_graph
  @metric = params[:project_data][:metric]
  @project_0 = params[:project_data][:project_0]
  @project_1 = params[:project_data][:project_1]
  @project_2 = params[:project_data][:project_2]
end

It is in this part of the logic flow where I need to populate the highchart with the information here.After the controller I move onto this js.erb file with this code: 在逻辑流程的这一部分中,我需要使用此处的信息填充高图。在控制器之后,我将使用以下代码移至此js.erb文件:

$('#graphModal').modal();

This snippet of code is from bootstrap and renders the highchart onto the modal div that has an id of #graphModal. 此代码段来自引导程序,并将高图表绘制到ID为#graphModal的模式div上。

This is where I am having problems. 这就是我遇到的问题。 The code in my compare.js file contains this: 我的compare.js文件中的代码包含以下内容:

 $(function() {
  new Highcharts.Chart({
    chart: {
      renderTo: "graphModal",
      height: 450,
      width: 700,
      borderWidth: 5,
      borderColor: '#525252'
    },
    title: {
      text: "Project Comparison Graph"
    },
    subtitle: {
      text: '# of contributors who made changes to the project source code each month'
    },
    series: [{
      // Look into pointStart and pointInterval
      // Also look into array of array values
      data: [1, 2, 3]
    }]
  });
});

For the life of me I can't figure out how to place my ruby code from the controller into this compare.js file. 对于我一生,我无法弄清楚如何将控制器中的红宝石代码放入此compare.js文件。 This doesn't work: 这不起作用:

(snippet of code) (代码段)

title: {
  text: @metric
 }

nor this (since I'm using haml): 也不是这样(因为我使用的是haml):

title: {
  text: = @metric
 }

Nor this in the js.erb file: js.erb文件中也没有:

var metric = <%= @metric %>;
$('#graphModal').modal();

When I do the code above, the chart ceases to render and metric is undefined despite my log saying that the modal rendered with a 200. 当我执行上述代码时,尽管我的日志显示使用200表示的模态,但该图表停止呈现,并且指标未定义。

I've tried inlining the code in my html.haml like so: 我尝试像这样在html.haml中内联代码:

  :ruby
  metric = opts[:graph][:metric] if opts[:graph].present?
  project_0 = opts[:graph][:project_0] if opts[:graph].present? && opts[:graph][:project_0].present?
  project_1 = opts[:graph][:project_1] if opts[:graph].present? && opts[:graph][:project_1].present?
  project_2 = opts[:graph][:project_2] if opts[:graph].present? && opts[:graph][:project_2].present?

:javascript
 $(function() {
  new Highcharts.Chart({
    chart: {
      renderTo: "graphModal",
      height: 450,
      width: 700,
      borderWidth: 5,
      borderColor: '#525252'
    },
    title: {
      text: "Project Comparison Graph"
    },
    subtitle: {
      text: metric #ruby code goes here.
    },
    series: [{
      // Look into pointStart and pointInterval
      // Also look into array of array values
      data: [1, 2, 3]
    }]
  });
});

I'm out of options. 我没办法了。 Why is none of these methods working. 为什么这些方法都不起作用。 Lastly, I've tried a javascript_tag block but that isn't working. 最后,我尝试了一个javascript_tag块,但这没有用。 Any help? 有什么帮助吗?

Remember that your ruby code and the Javascript code are not evaluated at the same time at all: 请记住,您的ruby代码和JavaScript代码根本不会同时评估:

  • your server reads the ruby code and generate HTML and Javascript 您的服务器读取ruby代码并生成HTML和Javascript
  • your browser only receive HTML and Javascript and evaluate it 您的浏览器仅接收HTML和Javascript并对其进行评估

In your case: 在您的情况下:

# in controller
@metric = 'kilometers'
# in view
var metric = <%= @metric %>;
# will output in javascript
var metric = kilometers;
# but `kilometers` is not declared as a JS string

Don't forget the quotes if you want to print a value as a string in js: 如果要在js中将值打印为字符串,请不要忘记引号:

# with ERB
var metric = '<%= @metric %>';
# with HAML
metric = '#{@metric}'

Which will output in javascript: 将在javascript中输出:

var metric = 'kilometers';

As we wanted in the first place. 首先我们想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM