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

时间:2016-05-14 01:48:23

标签: javascript jquery ruby-on-rails ruby ruby-on-rails-3

我想使用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> ");

但即使在控制台中显示

,也没有任何显示

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)

发布请求的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不会发送到localhost

1 个答案:

答案 0 :(得分:0)

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

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

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