Rails ajax调用一起生成404和500错误

时间:2017-05-16 20:10:39

标签: jquery ruby-on-rails json ruby ajax

我有两个动作,每个动作都在不同的控制器中,返回json数据。其中一个动作用于生成数据以绘制chartkick.js图。每5秒钟需要调用这些操作来更新图形和某些div上显示的数据。 我采取这些行动的途径是:

的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"

在我在视图上使用的javascript文件(static / index.html)上,我正在做下面的渲染图形和来自数据库的一些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 个答案:

答案 0 :(得分:0)

嗯,答案是,由于某种原因,调用ajax的操作包括JSON上的id。在模型中,我使用“@levels”作为返回而不是“级别”。当我只使用“级别”时,它起作用了。