简体   繁体   中英

Rails ajax calls generate 404 and 500 errors when together

I have two actions, each in a different controller, that return json data. One of these actions is used to generate data to plot a chartkick.js graph. Every 5 seconds these actions need to be called to update data that is showed on the graph and on some divs. My routes for these actions are:

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"

On my javascript file that is used on the view (static/index.html), I am doing like bellow to render the graph and the contents of some divs that are coming from the database:

assets/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);
    }
  });
}

I need that these actions to happen together. But this way I am getting this on my console and it keeps happening every 5 seconds on the ajax calls: 在此处输入图片说明

If I call just getLevels() to get div content and then keep it updating like this,

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

or if I just render the graph and keep it updating, like this,

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

they work perfectly. So I am able to call them separately but not able to use them together inside $(document).ready. Why is that happening?

The controllers code are:

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

The server log shows what is following when the actions are called consecutively:

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'

Well, the answer was that, for some reason, calling the actions by ajax was including the ids on the JSON. In the model, I was using "@levels" as a return instead of "levels". When I used just "levels", it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM