如何从活动管理员中的关联中选择所有记录



我有一些这样的模型:

class Sponsored < ActiveRecord::Base
has_many  :sponsored_sports
has_many  :sports,
through: :sponsored_sports,
class_name: 'Sport',
source: 'sport'
...
end

这是运动模型:

class Sport < ActiveRecord::Base
has_many :sponsored_sports
...
end

目前,在赞助的活动管理页面中,我通过以下代码一一创建赞助运动:

form do |f|
f.inputs "Details" do
...
f.has_many :sponsored_sports, heading: '', allow_destroy: true do |e|
e.input :sport_id, as: :select, :collection => Sport.order('rank'), :label_method => :name, :value_method => :name, :include_blank => false
end
end
f.actions
end

但是现在我只想为用户添加更多选项,通过单击复选框一次选择所有运动,例如:select all.那么如何在活动管理员中做到这一点呢?提前谢谢。

您可以在f.has_many窗体中添加按钮或链接并制作 javascript,以便在单击此链接时,可以通过编程方式将所有值添加到选择中。

或者,您可以将新参数发送到表单(如select_all(,并在active_admin控制器部分中更新或创建模型时进行关联:

controller do
def update
super
*check the params and make the association*
end
end

最新更新