Request via Ajax not returning filtered data

0

I am trying to return data via Ajax to load a select of states in Rails, so that only returns the states that belong to a given Country, but the return never arrives in the conditional, always to the parse of the JSON that I'm requesting from the API , anyone who can give me a light?

Jquery script to load select:

$('#pais_id').on('change', function() {
$.ajax({
  url:'/home/ajax_estados?id_pais=' + this.value,
  type:'GET',
  dataType:'json',
  success:function(estados){
    $("#estado_id").html("");
    $("#estado_id").append("<option value=''> Selecione </option>");
    $.each(estados.estados, function(index, estado) {
      $("#estado_id").append("<option value='"+estado.id+"'> "+estado.abreviacao+" </option>");
    });
  },
  error:function(data){
     $("#estado_id").html("");
     $("#estado_id").append("<option value=''> Selecione </option>");
  }
});
});

Action that is performed by Script:

  def ajax_estados
estados = Uf.buscar(params[:id_pais])
render json: {
  estados: estados
}.to_json
end

Uf class fetch method that loads the variable states in the above action:

def self.buscar(id)
uri = URI.parse(URI.escape("#{API}/ufs.json"))
request = HTTParty.get(uri)
if (200...300).include?(request.code.to_i)
  estados = JSON.parse(request.body);
  if estados.count > 0
    estados.each do |estado|
      return estado if estado["countrie_id"] == id
    end
  end
end
end

The parent id is being sent correctly, the return never goes from estados = JSON.parse(request.body); to fetch method.

    
asked by anonymous 08.10.2018 / 22:38

1 answer

0

I was able to solve it, the problem was that the parameter passed to the fetch method was sent as a string, and countrie_id is an integer, so the values never hit and it returned the last true value of the function. >     

10.10.2018 / 00:17