2017-05-16 2 views
0

それぞれが異なるコントローラに2つのアクションを持ち、jsonデータを返します。これらのアクションの1つは、chartkick.jsグラフをプロットするためのデータを生成するために使用されます。 5秒ごとにこれらのアクションを呼び出してグラフや一部のdivに表示されるデータを更新する必要があります。これらのアクションのための私のルートがある :ビューに使用されている私のjavascriptのファイル(静的/ index.htmlを)オンRailsのajax呼び出しは一緒になって404と500のエラーを生成します

routes.rbを

get "/get_all_tanks_graph", to: "charts#get_all_tanks_graph", as: "get_all_tanks_graph" 
get "/render_all_current_levels", to: "static#render_all_current_levels", as: "render_all_current_levels" 

、私はグラフを描画するには以下のようにやっていると、データベースから来ているいくつかのdivの内容:

資産/ static.js

$(document).ready(function() { 
    getLevels(); 
    getGraph(); 
    setInterval(
    function() { 
     getLevels(); 
     updateGraph(); 
    }, 
    5000 
); 
}); 

function getLevels() { 
    $.ajax({ 
    type: "GET", 
    url: "/render_all_current_levels", 
    dataType: "json", 
    success: function(response){ 
    ... 
    } 
    }); 
} 

function getGraph() { 
    $.ajax({ 
    type: "GET", 
    url: "/get_all_tanks_graph", 
    dataType: "json", 
    success: function(response){ 
     $chart = new Chartkick.LineChart("graph-display", response); 
    } 
    }); 
} 

function updateGraph() { 
    $.ajax({ 
    type: "GET", 
    url: "/get_all_tanks_graph", 
    dataType: "json", 
    success: function(response){ 
     $chart = Chartkick.charts["graph-display"]; 
     $chart.updateData(response); 
    } 
    }); 
} 

私はこれらのアクションは一緒に発生することが必要です。しかし、この方法では、私は私のコンソール上でこれを取得していますし、それは、Ajaxの呼び出しで5秒ごとに起こって保持します、 enter image description here

私はちょうどgetLevelsを()divのコンテンツを取得し、このように更新し、それを維持するために呼び出す場合

$(document).ready(function() { 
    getLevels(); 
    setInterval(
    function() { 
     getLevels(); 
    }, 
    5000 
); 
}); 

または私はちょうどグラフを描画し、このように、更新し、それを続ければ、

$(document).ready(function() { 
    getGraph(); 
    setInterval(
    function() { 
     updateGraph(); 
    }, 
    5000 
); 
}); 

は彼らが完璧に動作します。だから私はそれらを別々に呼び出すことができますが、$(document).readyの中でそれらを一緒に使うことはできません。なぜそれが起こっているのですか?

コントローラのコードは以下のとおりです。

def get_all_tanks_graph 
    @hash = {} 
    @array = Array.new 
    @levels = Level.get_all_tanks_levels 

    @levels.each do |l| 
     l.each do |i| 
     @hash[i.created_at] = i.level 
     end 
     @array << @hash 
     @hash = Hash.new 
    end 

    render json: @array.each_with_index.map { 
     |a, index| { 
     name: "Caixa #{index + 1}", data: a 
     } 
    } 

    end 

def render_all_current_levels 
    @levels = Array.new 
    array = Level.get_all_current_levels 
    array.each do |level| 
     @levels << Level.find(level) if level 
    end 
    render json: @levels 
    end 

サーバのログはアクションが連続して呼び出されたときに、次のされているものを示しています。

NoMethodError (undefined method `each' for 15:Fixnum): 


Completed 500 Internal Server Error in 11ms (ActiveRecord: 4.7ms) 



NoMethodError (undefined method `each' for 15:Fixnum): 

app/controllers/charts_controller.rb:9:in `block in get_all_tanks_graph' 
app/controllers/charts_controller.rb:8:in `each' 
app/controllers/charts_controller.rb:8:in `get_all_tanks_graph' 


Completed 404 Not Found in 51ms (ActiveRecord: 44.1ms) 



ActiveRecord::RecordNotFound (Couldn't find Level with 'id'=#<Level::ActiveRecord_Relation:0x007f9f36429ca8>): 

app/controllers/static_controller.rb:21:in `block in render_all_current_levels' 
app/controllers/static_controller.rb:20:in `each' 
app/controllers/static_controller.rb:20:in `render_all_current_levels' 
+1

500と404は、サーバーによって生成された状態です。あなたのサーバーログには何が入っていますか? – Puhlze

+0

@Puhlze、私はサーバーのログ情報で質問を編集しました。アクションが一緒に呼び出されないときは起こらないので、それは奇妙です。 –

+1

これらのコントローラのサーバコードを掲載することはできますか? – Puhlze

答えて

0

まあ、答えはいくつかの理由で、呼び出す、ということでしたajaxのアクションには、JSON上にidが含まれていました。モデルでは、 "レベル"の代わりに "@levels"をリターンとして使用していました。私がちょうど "レベル"を使用したとき、それは働いた。

関連する問題