2012-07-23 21 views
2

相關記錄我有這兩款車型創建新的關聯,同時創造積極管理

class Case < ActiveRecord::Base 
    belongs_to :client, :class_name => 'User' 
end 

class User < ActiveRecord::Base 
    has_one :requested_case, :class_name => 'Case', :foreign_key => :requested_case_id 
end 

,我想創建一個案例模型adminstration接口使用Active聯繫,所以當我創造新的情況下,我可以創造在同一時間爲它新的客戶,所以我寫的代碼如下線路中的應用程序/管理/ cases.rb文件

ActiveAdmin.register Case do 
    form do |f| 
     f.inputs "Basic Details" 
      f.input :title 
      f.input :Description 
     end 

     f.inputs :name => "Client Details", :for => :client do |c| 
      c.input :name 
      c.input :mobile 
     end 
     f.buttons 
    end 
end 

所以當我申請的客戶端的輸入,然後單擊提交我得到這個錯誤

ActiveRecord::AssociationTypeMismatch in Admin::CasesController#create 
User(#-625154418) expected, got ActiveSupport::HashWithIndifferentAccess(#82665960) 

所以任何幫助,請在這裏缺少什麼?

回答

2

只需添加到您的app/admin/cases.rb文件

controller do 
    def new 
     @case = Case.new 
     @case.build_client 
    end 
end 

,不要忘記添加accepts_nested_attributes_forcase模型

accepts_nested_attributes_for :client