vendredi 13 mai 2016

js.erb file not rendering its contents

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.

  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

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

def send_results(results):
    url = "http://ift.tt/226lSbH"
    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

Aucun commentaire:

Enregistrer un commentaire