我想使用js.erb呈現谷歌圖表。我從python腳本發出POST請求,將一些參數發送給我的ruby on rails應用程序,該應用程序解析這些參數並使用它們顯示我想要的圖形,但它不顯示任何內容。js.erb文件未呈現其內容
params_list = []
params.each_pair {|key, value|
params_list.push([key,value])
}
p params_list
@image = Gchart.pie_3d(:size => '500x300',
:title => "Top 5 Results",
:legend => ["#{params_list[0][0]} #{params_list[0][1]}%" , 'source-document2 [22%]','source-document3','source-document4','source-document5'],
:data => [22, 21, 20, 18, 19])
render :template => "deploy/results.js.erb", :locals => {:image => @image }
results.js.erb
$("#chart").append("<p> <%= escape_javascript render(partial: 'home/chart', locals: { :image => @image }) %> </p> ");
,但沒有顯示出來,即使在控制檯它說
Rendered deploy/results.js.erb
Rails的控制檯輸出
Started POST "/results.js" for 131.227.46.134 at 2016-05-14 03:18:13 +0100
Processing by DeployController#results as JS
Parameters: {"results"=>"[('source-document01348.txt', 22), ('source-document01389.txt', 21), ('source-document01253.txt', 20),
('source-document01306.txt', 19), ('source-document01255.txt', 18)]"}
Rendered home/_chart.html.erb (0.1ms)
Rendered deploy/results.js.erb (1.7ms)
Completed 200 OK in 9ms (Views: 8.4ms | ActiveRecord: 0.0ms)
的POST請求Python代碼
def send_results(results):
url = "http://e786a82b.ngrok.io/results.js"
data = {
"results": results
}
data = urllib.urlencode(data)
req = urllib2.Request(url, data)
# try:
response = urllib2.urlopen(url, data)
注意:使用ngrok創建一個隧道,如蟒蛇不會發送到本地主機
你能顯示你在Python腳本中收到的POST輸出嗎?你能展示整個行動的輸出嗎? –
@MichaelGaskill我發佈的軌道輸出,當它收到發佈請求,不介意渲染home_chart,我只是改變了js.erb我的JavaScript代碼呈現部分。 –