js.erb文件无法呈现其内容

时间:2022-11-07 08:42:28

I want to render a google chart using js.erb. I make a POST request from a python script, to send some parameters to my ruby on rails app which parses those parameters and uses them to show the graph i want but it does not show anything.

我想使用js.erb渲染谷歌图表。我从python脚本发出POST请求,将一些参数发送到我的ruby on rails app,它解析这些参数并使用它们来显示我想要的图形,但它没有显示任何内容。

  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> ");

but nothing shows up even though in console it says

但即使在控制台中也没有显示出来

Rendered deploy/results.js.erb

Rails console output

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)

python code for post request

邮件请求的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)

note: using ngrok to create a tunnel, as python does not send to localhost

注意:使用ngrok创建隧道,因为python不会发送到localhost

1 个解决方案

#1


0  

Instead of explicitly referring to your partial by filename, try this:

不要明确引用您的部分文件名,请尝试以下方法:

render "deploy/results", :formats => [:js], :locals => {:image => @image }

Rails doesn't know what you're trying to render by filename, it does it by context and format. Specifying the :formats option helps Rails to know your intent.

Rails不知道你试图通过文件名呈现什么,它是通过上下文和格式来实现的。指定:formats选项可帮助Rails了解您的意图。

#1


0  

Instead of explicitly referring to your partial by filename, try this:

不要明确引用您的部分文件名,请尝试以下方法:

render "deploy/results", :formats => [:js], :locals => {:image => @image }

Rails doesn't know what you're trying to render by filename, it does it by context and format. Specifying the :formats option helps Rails to know your intent.

Rails不知道你试图通过文件名呈现什么,它是通过上下文和格式来实现的。指定:formats选项可帮助Rails了解您的意图。