【问题标题】:Using HABTM or Has_many through with Active Admin通过 Active Admin 使用 HABTM 或 Has_many
【发布时间】:2013-05-02 05:19:39
【问题描述】:

我已经阅读了很多关于通过关联使用带有 has_many 的活动管理员的帖子,但我没有得到想要的结果。本质上,我有 2 个模型“会议”和“帐户”。我需要将多个帐户分配给一个会议,并将多个会议分配给一个帐户。无论我使用 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)>'

我该如何解决这个问题?

谢谢。

【问题讨论】:

  • 嗨,你为什么在模型中使用:through:through用于多对多关联,例如:has_many :replies, :through =&gt; :articles, :source =&gt; :comments :replies 是 call 的别名

标签: ruby-on-rails activeadmin has-many-through formtastic


【解决方案1】:

您是否尝试将以下几行添加到您的会议模型中?

# 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

【讨论】:

  • 我试过了,但是当我尝试创建一个新的会议对象时,我仍然遇到同样的错误。此外,在显示页面上没有任何内容可供过滤
  • 啊,应该是“conferenceaccounts”,你可能需要在会议模型中添加 has_many :conferenceaccounts。已修复,请立即尝试 :)
  • 顺便说一句,您要添加哪些过滤器?您是指索引页面上的过滤器?
【解决方案2】:

使用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

【讨论】:

    猜你喜欢
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多