2012-08-03 40 views
0

我無法弄清楚我做錯了什麼。我有幾個選擇列表:Rails 2和ajax - 很少的選擇列表

  • 地區
  • 城鎮
  • 組織

應該如何工作:

  1. 用戶選擇區域
  2. 城鎮名單負載。用戶選擇
  3. 列表組織負載。用戶選擇組織

我使用ajax的,我意識到只有自動加載的城鎮名單,我不能做同樣的事情組織。第二個選擇列表根本不會發布

這裏是我的代碼:

查看

%div 
    %br/ 
    = select 'ajax', :region_id, [['Choose your region...', -1]] + Region.all.map{|region| [region.name, region.id]}.sort 
    = image_tag('ajax-loader.gif', :id => 'ajax-progress', :style => 'display: none;') 
    = observe_field :ajax_region_id, :url => { :controller => :organizations, :action => :list_towns, :preselect => (defined?(preselect) && !preselect.nil?) }, :with => 'region_id', :update => 'select_organization_idd', :loading => '$("ajax-progress").show();', :complete => 'if (Ajax.activeRequestCount == 1) $("ajax-progress").hide();' 
    %br/ 
    = select 'ajax2', :o_id, [], {}, {:id => 'select_organization_idd', :style => 'width: 60em;'} 
    = image_tag('ajax-loader.gif', :id => 'ajax-progress', :style => 'display: none;') 
    = observe_field :ajax_region2_id, :url => { :controller => :organizations, :action => :list_region, :preselect => (defined?(preselect) && !preselect.nil?) }, :with => 'o_id', :update => 'select_organization_idd2', :loading => '$("ajax-progress").show();', :complete => 'if (Ajax.activeRequestCount == 1) $("ajax-progress").hide();' 

    = f.select :organization_id, [], {}, {:id => 'select_organization_idd2', :style => 'width: 60em;'} 

控制器

def list_region 
    render :text => ActionController::Base.helpers.options_for_select((params[:preselect] ? [['Choose organization', -1]] : []) + Organization.map{|org| [ActionController::Base.helpers.truncate(org.name, :length => 155), org.id]}) 
    end 

    def list_towns 
    region = Region.find(params[:region_id]) 
    towns = Kladr.find(:all, :conditions => ['code LIKE ?', "#{region.code}%"]) 
    render :text => ActionController::Base.helpers.options_for_select([['Choose town', -1]] + towns.map{ |t| ["#{t.socr}. #{t.name}", t.id] }) 
    end 

我該怎麼辦錯了嗎?另外我使用Rails 2,爲什麼observe_field不被棄用。

回答

1

你使用導軌3嗎?方法observe_field已棄用。

了Rails 3,你必須做這樣的事情:

視圖

%div 
    %br/ 
    = select 'ajax', :region_id, [['Choose your region...', -1]] + Region.all.map{|region| [region.name, region.id]}.sort, {}, {:id => 'ajax1'} 
    = image_tag('ajax-loader.gif', :id => 'ajax-progress', :style => 'display: none;') 
    %br/ 
    = select 'ajax2', :o_id, [], {}, {:id => 'select_organization_idd', :style => 'width: 60em;'}, {}, {:id => 'ajax2'} 
    = image_tag('ajax-loader.gif', :id => 'ajax-progress', :style => 'display: none;') 

    = f.select :organization_id, [], {}, {:id => 'select_organization_idd2', :style => 'width: 60em;'} 

javascript.js.coffee

$('#ajax1').change =() -> 
    $.post(
    url: '/organizations/list_towns' 
    data: {value: $('#ajax1').val()} 
    success:() -> 
     if (Ajax.activeRequestCount == 1) 
     $("ajax-progress").hide() 
) 

$('#ajax2').change =() -> 
    $.post(
    url: '/organizations/list_regions' 
    data: {value: $('#ajax2').val()} 
    success:() -> 
     if (Ajax.activeRequestCount == 1) 
     $("ajax-progress").hide() 
) 

的實施可能是錯誤的,可能是重構。我沒有測試它。但你有想法。

+0

我忘了提及我使用Rails 2. – ExiRe 2012-08-03 14:40:23

+0

請編輯您的問題。 (你應該更新到rails 3(或4))。 – Dougui 2012-08-03 14:44:29

+0

您也可以在rails 2項目中包含JQuery。 – Dougui 2012-08-03 14:52:39