通过Active Admin使用HABTM或Has_many [英] Using HABTM or Has_many through with Active Admin

查看:80
本文介绍了通过Active Admin使用HABTM或Has_many的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多关于通过关联将有效管理与has_many结合使用的信息,但是我没有得到预期的结果。本质上,我有2个模型会议和 帐户。我需要为一个会议分配多个帐户,并为一个帐户分配多个会议。我使用HABTM还是has_many都与我无关。在帐户上创建新会议时,我只需要能够看到一个下拉选择选项,反之亦然。

I've read quite a few of the posts on using active admin with has_many through association but I'm not getting the desired results. Essentially I have 2 models "Conferences" & "Accounts". I need to assign multiple accounts to a conference and multiple conferences to an account. Wether I use HABTM or has_many through doesn't matter to me. I just need to be able to see a dropdown select option when I create a new conference and vice versa on accounts.

帐户模型

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)>'

我该如何解决?

谢谢。

推荐答案

您尝试在会议模型中添加以下几行吗?

Did you try to add below lines to your Conference model?

# conference.rb
attr_accessible : conferenceaccounts_attributes
has_many :conferenceaccounts

# This line after your your relations
accepts_nested_attributes_for : conferenceaccounts, :allow_destroy => true

请参阅:接受has_many关系的嵌套属性

这篇关于通过Active Admin使用HABTM或Has_many的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆