2013-10-08 66 views
0

有三個國家,州和城市的下拉框。Rails 3保留了驗證失敗後由ajax填充的下拉框的值

.entry.float 
%label{:for => "hospital-country"} Country 
= select_tag :country_id, options_for_select(["Select option"] + @countries.collect{ |c| [c.name, c.id] },@hospital.country_id),{:name =>"hospital[country_id]",:class => "select r5",:id => "hospital-country",:onchange=>"country_change()"} 

.entry.float 
%label{:for => "hospital-state"} State 
= select_tag :state, options_for_select(["Select option"]),{:name =>"hospital[state]",:id => 'hospital-state',:class => "select r5",:disabled => true,:onchange=>"state_change()"} 

.entry.float 
%label{:for => "hospital-city"} City 
= select_tag :city, options_for_select(["Select option"]),{:name =>"hospital[city]",:id => 'hospital-city',:class => "select r5",:disabled => true} 

在哪個國家被填充`

@countries = WorldCountry.all

` 和狀態和城市由國家和國家下拉框的AJAX調用分別填充使用jquery

function country_change() { 
    $('#hospital-state').find('option[value!="Select option"]').remove(); 
    $('#hospital-state').next().text("Select option"); 
    $('#hospital-state').attr('disabled', 'disabled'); 
    $('#hospital-city').find('option[value!="Select option"]').remove(); 
    $('#hospital-city').next().text("Select option"); 
    $('#hospital-city').attr('disabled', 'disabled'); 
    if ($('#hospital-country').val() != "" && $('#hospital-country').val() != "Select option") { 
     $.ajax({ 
      type : 'post', 
      url : '/hospitals/country_change', 
      data : { 
       country_id : $('#hospital-country').val() 
      }, 
      dataType : "json", 
      success : function(data) { 
       var select = $('#hospital-state'); 
       if (data.length > 0) { 
        select.removeAttr('disabled'); 
        $.each(data, function(key, value) { 
         $("<option/>").val(value[1]).text(value[0]).appendTo(select); 
        }); 
       } 
      }, 
      failure : function() { 
      } 
     }) 
    } 
}; 

所有這些工作。但是,當驗證失敗時,我只能保留國家的價值。 如何保留僅適用於ajax的州和城市的價值。如果驗證失敗

@states =WorldRegion.where("world_country_id=?",params[:hospital][:country_id]) 
@selected_state = WorldRegion.find_by_id(params[:hospital][:state]).id rescue nil 
@cities = WorldCity.where("world_country_id=? and world_region_id=?",params[:hospital][:country_id],params[:hospital][:state]) 
@selected_city = WorldCity.find_by_id(params[:hospital][:city]).id rescue nil  
render action: "new" 

它工作正常

+0

您不會將所選值傳遞給城市和州的'options_for_select'。所以不會有任何選擇。 – phoet

回答

0

更改視圖

.entry.float 
%label{:for => "hospital-state"} State 
-if [email protected]? 
    = select_tag :state, options_for_select(["Select option"] + @states.collect{ |s| [s.name, s.id] },@selected_state),{:name =>"hospital[state]",:class => "select r5",:id => "hospital-state",:onchange=>"state_change()"} 
- else 
    = select_tag :state, options_for_select(["Select option"]),{:name =>"hospital[state]",:id => 'hospital-state',:class => "select r5",:onchange=>"state_change()"} 
.entry.float 
%label{:for => "hospital-city"} City 
- if [email protected]? 
= select_tag :city, options_for_select(["Select option"] + @cities.collect{ |c| [c.name, c.id] },@selected_city),{:name =>"hospital[city]",:class => "select r5",:id => "hospital-state"} 
-else 
= select_tag :city, options_for_select(["Select option"]),{:name =>"hospital[city]",:id => 'hospital-city',:class => "select r5",:disabled => true} 

而在CONTROLER 。 謝謝 hari