[英]Gon variable is not defined in js.erb file using ajax
I have a highchart that is being rendered by a js.erb
file in conjunction with the gon
variable. 我有一个js.erb文件与gon
变量一起呈现的js.erb
。 The setup that I have is that a user will load a page and once the page is ready an ajax call will be made to a separate controller. 我的设置是用户将加载页面,页面准备就绪后,将向单独的控制器进行ajax调用。 The controller will handle the data needed, assign it to gon and will render the appropriate js.erb
file with the data. 控制器将处理所需的数据,将其分配给gon,并将使用该数据呈现适当的js.erb
文件。 However, I keep observing that gon
is consistently undefined and the variables are not being sent to gon
. 但是,我一直观察到gon
始终未定义,并且变量没有发送到gon
。 I wonder if this has to do with ajax but here is my code and logic flow. 我想知道这是否与ajax有关,但这是我的代码和逻辑流程。
Here is where the call originates 这是电话的发起地
_vulnerabilites_per_version.html.haml
%script{:src => "https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"}
= Gon::Base.render_data
- vanity_url = project.vanity_url
:javascript
$(document).ready(function() {
$.ajax({
url: "/p/#{vanity_url}/security.js"
});
})
#vulnerabilities{:style => "max-width: 405px; height: 300px; margin: 0 auto"}
The call goes then to the controller 呼叫然后转到控制器
class VulnerabilitiesController < ApplicationController
before_action :set_project_or_fail
before_action :set_best_project_security_set
before_action :set_releases
before_action :set_vulnerabilities
def vulnerabilities_per_version
gon.watch.release_versions = @releases.map { |r| r.version }
puts gon.release_versions
end
private
def set_project_or_fail
project_id = params[:project_id] || params[:id]
@project = Project.by_vanity_url_or_id(project_id).take
raise ParamRecordNotFound unless @project
end
def set_best_project_security_set
@best_project_security_set = @project.best_project_security_set
end
def set_releases
@releases = @best_project_security_set.releases.order(released_on: :asc).limit(10)
end
def set_vulnerabilities
@vulnerabilities = Array.new(3) { [] }
@releases.each do |r|
@vulnerabilities[0] << r.vulnerabilities.low.count
@vulnerabilities[1] << r.vulnerabilities.medium.count
@vulnerabilities[2] << r.vulnerabilities.high.count
end
end
end
As you can see, the release_versions variable is being passed to gon
and I can view it in the logs that the values that I am looking for are there. 如您所见,release_versions变量正在传递给gon
并且我可以在日志中查看该变量,以查找所需的值。
Once we leave the controller, the js.erb
file is rendered: 离开控制器后,将呈现js.erb
文件:
vulnerabilites_per_version.js.erb
alert(gon.watch.release_versions)
$(function () {
$('#vulnerabilities').highcharts({
chart: {
type: 'column'
},
title: {
text: 'Vulnerabilities per Version',
align: 'left',
style: {
color: '#336699',
fontSize: 16
}
},
legend: {
align: 'left',
itemWidth: 127
},
xAxis: {
categories: gon.release_versions
},
yAxis: {
min: 0,
title: {
text: 'Vulnerabilities'
},
.........code.....
What could be the cause of this problem? 可能是这个问题的原因? Why am I unable to access the data in my js.erb
file? 为什么我无法访问js.erb
文件中的数据? According to the gon
documentation, I should have everything set in order for the data to render yet it doesn't. 根据gon
文档,我应该设置所有内容以使数据呈现,但事实并非如此。 As a second attempt, I also proceeded to read up on the gon.watch
usage but again no luck. 作为第二次尝试,我也继续阅读gon.watch
用法,但还是没有运气。 Any ideas? 有任何想法吗? Help on the matter would greatly be appreciated. 对此事的帮助将不胜感激。
I'm not sure what gon
is here but if you declare gon
as an instance variable ( @gon
), that should do the trick. 我不确定这里是什么gon
,但是如果您将gon
声明为实例变量( @gon
),那应该可以解决问题。
Controller 控制者
def show
@dog = Dog.first
cat = Cat.first
end
View: show.html.erb
查看: show.html.erb
@dog.name => "Sparky"
cat.name => undefined method 'name' for nil class
I suspect that your js.erb
file isn't updating the global gon
variable. 我怀疑您的js.erb
文件没有更新全局gon
变量。
Try setting the global gon variable in the template: 尝试在模板中设置全局gon变量:
#vulnerabilites_per_version.js.erb
gon = #{gon}
console.log(gon.watch.release_versions)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.