我有以下设置:
class User < ActiveRecord::Base
has_many :device_ownerships, :dependent => :destroy
has_many :devices, :through => :device_ownerships
end
class device < ActiveRecord::Base
has_one :device_ownership, :dependent => :destroy
has_one :user, :through => :device_ownership
end
class deviceOwnership < ActiveRecord::Base
belongs_to :user
belongs_to :device
validates_uniqueness_of :device_id, :scope => :user_id
validates_uniqueness_of :user_id, :scope => :device_id
end
我正在尝试在Active Admin中实现以下功能:
在编辑屏幕
1)列出属于用户的所有设备,并选择删除设备或销毁将设备连接到用户的deviceOwnership
2)可以选择从现有设备创建新的配对用户设备(通过创建新的DeviceOwnership
)。
3)可以选择创建新设备并通过新的DeviceOwnership
将其添加到用户。
我在下面的评论中列出了我现在所遇到的问题:
ActiveAdmin.register User do
permit_params :email, :password, :password_confirmation, :role,
device_ownerships_attributes: [:device_id, :user_id],
devices_attributes: [:device_identifier]
index do |user|
user.column :email
user.column :current_sign_in_at
user.column :last_sign_in_at
user.column :sign_in_count
user.column :role
actions
end
filter :email
form do |f|
f.inputs "User Details" do
f.input :email
f.input :password
f.input :password_confirmation
f.input :role, as: :radio, collection: {Editor: "editor", Moderator: "moderator", Administrator: "administrator"}
#This one allows to create new devices but also lists all existing devices with option to modify their device_identifier column which I don't want
f.has_many :devices, :allow_destroy => true, :heading => 'Themes', :new_record => true do |cf|
cf.input :device_identifier
end
#This one lists all the devices but no option to remove any of them.
f.input :devices
#This one shows dropdownw with existing devices but allows to swap them
f.has_many :devices, :allow_destroy => true do |device_f|
device_f.input :device_identifier, :as => :select, :collection => device.all.map{ |device| [device.device_identifier] }, include_blank: false,
end
f.actions
end
end
end
答案 0 :(得分:0)
该行
my_enum != ALL(enum_range(null::MyEnum))
看起来它可以完成这项工作。您可以检查cf.object是否是新记录,并且只允许用户在这种情况下更改f.has_many :devices, :allow_destroy => true, :heading => 'Themes', :new_record => true do |cf|
cf.input :device_identifier
end
。
device_identifier
或类似cf.input :device_identifier if cf.object.new_record?
您怎么看?