我已经通过关联阅读了很多关于使用带有has_many的活动管理员的帖子,但是我没有得到想要的结果。基本上我有两个模型“会议”& “帐户”。我需要为会议分配多个帐户,为帐户分配多个会议。我使用HABTM或has_many对我来说无关紧要。我只需要在创建新会议时看到下拉选择选项,反之亦然。
帐户模型
class Account < ActiveRecord::Base
attr_accessible :address, :city, :name, :phone, :state, :website, :zip
has_many :contacts, :dependent => :destroy
has_many :conferences, :through => :conferenceaccount
end
会议模式
class Conference < ActiveRecord::Base
attr_accessible :address, :city, :conferencename, :eventdateend, :eventdatestart, :industry, :phone, :state, :website
has_many :accounts, :through => :conferenceaccount
end
会议帐户模型
class Conferenceaccount < ActiveRecord::Base
belongs_to :conference
belongs_to :account
attr_accessible :account_id, :conference_id
end
会议管理模式
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)>'
我该如何解决这个问题?
感谢。
答案 0 :(得分:4)
您是否尝试在会议模型中添加以下行?
# conference.rb
attr_accessible : conferenceaccounts_attributes
has_many :conferenceaccounts
# This line after your your relations
accepts_nested_attributes_for : conferenceaccounts, :allow_destroy => true
答案 1 :(得分:1)
使用has_many through: :some_model
时,请务必在:some_model
上定义has_many,例如:
如果你有:
class Account < ActiveRecord::Base
has_many :conferences, :through => :conferenceaccount
end
将其转换为:
class Account < ActiveRecord::Base
has_many :conferences, :through => :conferenceaccount
has_many :conferenceaccount # <- added
end