0

我不知道我做错了什么。我有几个选择列表:

  • 地区
  • 城市
  • 组织

它应该如何工作:

  1. 用户选择区域
  2. 城镇负载列表。
  3. 用户选择城镇
  4. 组织负载列表。
  5. 用户选择组织

我为此使用了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不被弃用。

4

1 回答 1

1

你用rails 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()
  )

实现可能是错误的,可以重构。我没有测试它。但你有这个想法。

于 2012-08-03T14:29:22.923 回答