Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Can';在Active Admin(Rails 3.2,Active Admin 1.0)上的“我的从属选择”下拉列表中找不到错误_Javascript_Jquery_Ruby On Rails_Ruby On Rails 3_Activeadmin - Fatal编程技术网

Javascript Can';在Active Admin(Rails 3.2,Active Admin 1.0)上的“我的从属选择”下拉列表中找不到错误

Javascript Can';在Active Admin(Rails 3.2,Active Admin 1.0)上的“我的从属选择”下拉列表中找不到错误,javascript,jquery,ruby-on-rails,ruby-on-rails-3,activeadmin,Javascript,Jquery,Ruby On Rails,Ruby On Rails 3,Activeadmin,我正在尝试构建一个RoR应用程序,包括三种型号: 可分为一个部门(称为游戏部门)和一个子部门(称为游戏子部门)的游戏 一个部门由许多子部门组成 界别分组 以下是我的基本模型关系: 模型/游戏.rb belongs_to :game_sector, :foreign_key => 'game_sector_id', :counter_cache => true belongs_to :game_subsector, :foreign_key => 'game_sub

我正在尝试构建一个RoR应用程序,包括三种型号:

  • 可分为一个部门(称为游戏部门)和一个子部门(称为游戏子部门)的游戏
  • 一个部门由许多子部门组成
  • 界别分组
以下是我的基本模型关系:

模型/游戏.rb

belongs_to :game_sector,    :foreign_key => 'game_sector_id',   :counter_cache => true
belongs_to :game_subsector, :foreign_key => 'game_subsector_id',:counter_cache => true
我使用Active Admin输入游戏、部门或子部门信息

当我创建一个游戏时,我有一个非常基本的表单,我只想让第二个选择下拉列表(game_subsector)在第一个选择(gamesector)的选择上进行调整,这样我就不需要整个(很长)game_subsector列表,而只需要那些属于我选择的game_sector的列表

经过几十次测试和技术尝试,但都失败了,我终于使用了这个似乎与我相关的开发人员的建议:

但它仍然不起作用

这是位于Admin/game.rb上的Active Admin表单

ActiveAdmin.register Game do

  menu :parent => "Campaigns", :priority => 1

  controller do 
    with_role :admin_user      

      def game_subsectors_by_game_sector
      if params[:id].present?
        @game_subsectors = GameSector.find(params[:id]).game_subsectors
      else
        @game_subsectors = []
      end

      respond_to do |format|
        format.js
      end
    end

  end


form do |f|

    f.inputs "Details" do
      f.input :name
      f.input :game_sector_id,
        :label => "Select industry:",
        :as => :select, :collection => GameSector.all(:order => :name),
        :input_html => { :rel => "/game_sectors/game_subsectors_by_game_sector" }
      f.input :game_subsector_id, :as => :select, :collection => GameSubsector.all(:order => :name)

f.actions
  end
我觉得javascript甚至可能没有被激活

我使用的jquery位于
app/assets/javascript/admin/active_admin.js
(我更改了配置,以便在加载活动管理页面时加载此javascript)

最后,我创建了一个视图:in-app/views/layout/game\u subsectors\u by\u game\u sector.js.erb

$("#game_game_subsector_id").html('<%= options_for_select(@game_subsectors.map {|sc| [sc.name, sc.id]}).gsub(/n/, '') %>');
我不确定我是否把它放在了正确的地方…

您需要的是:

  • 使用web浏览器控制台检查您的选择,并使用CSS选择器为扇区选择创建jQuery对象,如:

    $('#sector_select')
    
  • 向该对象追加一个处理程序,以便在其更改时激发AJAX请求:

    $('#sector_select').change(function(){
      $.ajax('/subsectors/for_select', {sector_id: $(this).val()})
        .done(function(response){ // 3. populate subsector select
          $('#subsector_select').html(response);
        });
    });
    
  • 请参见代码中的3,您需要检查以获得正确的CSS选择器。确保您在web浏览器检查器的“网络”选项卡中得到预期的响应(如果使用Chrome)

  • 您需要在文件
    app/controllers/subsectors\u controller.rb
    中的
    /subsectors/for\u select
    中应答的控制器:

    class SubsectorsController  < ApplicationController
      def for_select
        @subsectors = Subsector.where sector_id: params[:sector_id]
      end
    end
    
    <% @subsectors.each do |ss| %>
      <option value="<%= ss.id %>"><%= ss.name %></option>
    <% end %>
    
  • 您需要一条路线:

    get '/subsectors/for_select', to: 'subsectors#for_select'
    
  • get '/subsectors/for_select', to: 'subsectors#for_select'