Javascript Can';在Active Admin(Rails 3.2,Active Admin 1.0)上的“我的从属选择”下拉列表中找不到错误
我正在尝试构建一个RoR应用程序,包括三种型号: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
- 可分为一个部门(称为游戏部门)和一个子部门(称为游戏子部门)的游戏
- 一个部门由许多子部门组成
- 界别分组
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/, '') %>');
我不确定我是否把它放在了正确的地方…您需要的是:
$('#sector_select')
$('#sector_select').change(function(){
$.ajax('/subsectors/for_select', {sector_id: $(this).val()})
.done(function(response){ // 3. populate subsector select
$('#subsector_select').html(response);
});
});
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'