第一次没有正确恢复Rails字段值

时间:2018-02-06 09:08:56

标签: javascript jquery ruby-on-rails

我的rails应用程序遇到了一些问题,我创建了一个级联城市选择系统,我们首先选择国家,然后我打一个内部电话来接收与该国家相关的州的州和然后,一旦选择了州,我对城市做同样的事情,除了一个小虫子,一切正常。

当我第一次加载页面时,我可以更改城市并正确保存。另一方面,如果我改变国家,国家和城市,那么国家应该得到保障,国家也是如此,而不是城市。当页面在保存城市后返回并且始终是列表中的第一个,并且只是在这个时刻,如果我只改变了城市,那么它将被保存得很好。

在红宝石方面,我在一个before_update中放了一个byebug,我注意到在第一个案例中,城市被严重保存的那个,它是我收到的城市的索引而不是城市的全名,而在另一种情况下,当它正确地保存自己时,它就是我得到的城市名称。

side js我在控制台发送的onchange事件中恢复了下拉列表的值,无论如何它都是出现的城市名称。

person_settings.js

// This is executed when contry_select is changed
$('#person_country_of_birth').on('change', function(event) {
  var stateInput = $("#person_state_of_birth");
  var country_code = $('#person_country_of_birth :selected').val();
  var sendDatas = { "country_code": country_code };

  getDatas(sendDatas, stateInput, "update_states");
});

// This is executed when state_select is changed
$('#person_state_of_birth').on('change', function(event) {
  var cityInput = $("#person_city_of_birth");
  var country_code = $('#person_country_of_birth :selected').val();
  var state_code = $('#person_state_of_birth :selected').val();
  var sendDatas = { "country_code": country_code, "state_code": state_code};

  getDatas(sendDatas, cityInput, "update_cities");
});

// Check value when change (test)
$('#person_city_of_birth').on('change', function(e) {
  console.log(this.options[e.target.selectedIndex].text)
  // $('#person_city_of_birth').text(this.options[e.target.selectedIndex].text);
});

// This function send selected data to get cascading response
function getDatas(sendDatas, inputTarget, urlAction){
  var url = window.location.origin + "/dynamic_select/"
  $.ajax({
    type: "GET",
    url: url + urlAction,
    dataType: "json",
    data: sendDatas,
    success: function(response){
        if(getType(response) == "[object Array]"){
          console.log(response)
          console.log(sendDatas)
          appendStateData(response, inputTarget);
        }
        else if(getType(response) == "[object Object]"){
          appendCityData(response, inputTarget);
        }
    },
    error: function(resp) {
      alert(resp)
      console.log(resp)
    }        
  });
};

// Append states datas on state_select
function appendStateData(datas, state_input){
  state_input.empty();
  $.each(datas, function(index, value) {
    state_input.append("<option value="+index+">"+value+"</option>");
  }); 
};

// Append cities datas on city_select
function appendCityData(datas, city_input){
  city_input.empty();
  $.each(datas, function(index, value) {
    city_input.append("<option value="+index+">"+value+"</option>");
  }); 
};

// This function check the type of response
// State datas is an object
// City datas is an array
function getType( obj ){
  return Object.prototype.toString.call(obj).toString();
};

contact.haml

.row
  .col-6
    .inline-label-container
      = form.label :spoken_languages, @spoken_languages_title
    %span.alert-box-icon
      = icon_tag("information", ["icon-fix"])
    %small
      = t("settings.profile.spoken_languages_description")
    = form.select(:spoken_languages, @spoken_languages_datas.each { |p| [p] }, {prompt: 'Select a language'}, {multiple: true, size: @spoken_languages_datas.count, style: 'height: 180px;'})

  .col-3
    = form.label :birth_date, t("settings.profile.birth_date")
    = form.date_field :birth_date, :class => "date_field", :maxlength => "10", paceholder: "30/12/2018", style: "height: 40px; width: 250px;"
  .col-3
    = form.label :nationality, t("settings.profile.nationality")
    = form.select(:nationality, @nationality_datas)
  .col-3
    = form.label :country_of_birth, t("settings.profile.country_of_birth")
    = form.country_select :country_of_birth, priority_countries: eu_countries_codes, include_blank: false
  .col-3
    = form.label :state_of_birth, t("settings.profile.state_of_birth")
    = form.select(:state_of_birth, CS.states(target_user.country_of_birth).each { |sym, state| [sym, state]}.to_a.map {|arr| arr.reverse!})
  .col-6
    = form.label :city_of_birth, t("settings.profile.city_of_birth")
    = form.select(:city_of_birth, CS.cities(target_user.state_of_birth, target_user.country_of_birth))

1 个答案:

答案 0 :(得分:0)

它解决了,问题来自这样一个事实:对于城市来说,它是一个数组,而不是我从命中获得的哈希,索引被用作值,我添加了一个javascript方法,在我之后为我做转换得到答案,一切正常。

// This function covert array into object 
// and set keys == value ex : {lisboa: "lisboa"}
function toObject(arr) {
  console.log(arr)
  var rv = {};
  for (var i = 0; i < arr.length; ++i)
    rv[arr[i]] = arr[i];
  console.log(rv)
  return rv;
}