【问题标题】:Rails 2 and ajax - few select listsRails 2 和 ajax - 几个选择列表
【发布时间】:2012-08-03 13:45:12
【问题描述】:

我不知道我做错了什么。我的选择列表很少:

  • 地区
  • 城镇
  • 组织

它应该如何工作:

  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 没有被弃用。

【问题讨论】:

    标签: ruby-on-rails ruby ajax haml


    【解决方案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()
      )
    

    实现可能是错误的,可以重构。我没有测试它。但你明白了。

    【讨论】:

    • 我忘了说我使用的是 Rails 2。
    • 请编辑您的问题。 (你应该更新到 rails 3(或 4))。
    • 你也可以在 rails 2 项目中包含 JQuery。
    • 我更新了我的问题。不幸的是,我无法升级到 Rails 3,因为项目太旧、太大并且依赖项太多。
    猜你喜欢
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    相关资源
    最近更新 更多