我已经阅读了很多关于通过关联将事件管理员与 has_many 结合使用的帖子,但我没有得到想要的结果。基本上我有 2 个模型“ session ”和“帐户”。我需要为一个 session 分配多个帐户,为一个帐户分配多个 session 。我是否使用 HABTM 或 has_many through 对我来说并不重要。我只需要能够在创建新 session 时看到下拉选择选项,反之亦然。
账户模型
class Account < ActiveRecord::Base
attr_accessible :address, :city, :name, :phone, :state, :website, :zip
has_many :contacts, :dependent => :destroy
has_many :conferences, :through => :conferenceaccount
end
session 模型
class Conference < ActiveRecord::Base
attr_accessible :address, :city, :conferencename, :eventdateend, :eventdatestart, :industry, :phone, :state, :website
has_many :accounts, :through => :conferenceaccount
end
session 账号模型
class Conferenceaccount < ActiveRecord::Base
belongs_to :conference
belongs_to :account
attr_accessible :account_id, :conference_id
end
session 管理模式
ActiveAdmin.register Conference do
form do |f|
f.inputs "Details" do # Project's fields
f.input :conferencename
f.input :address
f.input :city
f.input :state
f.input :website
f.input :phone
f.input :eventdatestart
f.input :eventdateend
f.input :industry
end
f.has_many :conferenceaccounts do |app_f|
app_f.inputs "Conferences" do
if !app_f.object.nil?
# show the destroy checkbox only if it is an existing appointment
# else, there's already dynamic JS to add / remove new appointments
app_f.input :_destroy, :as => :boolean, :label => "Destroy?"
end
app_f.input :account # it should automatically generate a drop-down select to choose from your existing patients
end
end
f.buttons
end
end
我不断收到以下错误
ActionView::Template::Error (undefined method `klass' for nil:NilClass):
1: insert_tag renderer_for(:new)
app/admin/conferences.rb:14:in `block (2 levels) in <top (required)>'
我怎样才能解决这个问题?
谢谢。
最佳答案
您是否尝试将以下几行添加到您的 session 模型中?
# conference.rb
attr_accessible : conferenceaccounts_attributes
has_many :conferenceaccounts
# This line after your your relations
accepts_nested_attributes_for : conferenceaccounts, :allow_destroy => true
见:accept nested attributes for has_many relationship
关于ruby-on-rails - 通过 Active Admin 使用 HABTM 或 Has_many,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16432857/