2015-01-03 23 views
1

我想實現嵌套使用reform寶石。我有三個型號:Rails 4與改革創業板和simple_form

  1. 客戶(belongs_to的:personal_details)
  2. PersonalDetail(belongs_to的:標題)
  3. 標題(的has_many:PersonalDetail)

我試圖創建一個使用一個CustomerForm使用以下代碼改革寶石:

/app/forms/customer_form.rb 
class CustomerForm < Reform::Form 
property  :national_id 
validates  :national_id, 
      :presence => true 

property :personal do 

property  :first_name 
property  :title 


validates  :first_name, 
       :presence => true 


end 
end 


/app/controllers/customers_controller.rb 
def new 
    @form = CustomerForm.new(Customer.new(personal: Personal.new)) 
end 


/app/views/customers/new.html.haml 
= simple_form_for @form, :url => customers_path do |f| 
    = f.input   :national_id 
    = f.simple_fields_for :personal do |builder| 
    = builder.input   :first_name 
    = builder.association  :title 
    = f.button :submit, "Submit" 

:title屬性將不會收到任何inpu t在表單提交期間。 Title和PersonalDetail之間的關係僅用於使用Title.name列表填充下拉列表。如果我嘗試上述操作,則會收到一條錯誤消息,指出沒有關聯:存在性別。當我使用標準的基於模型的方法嘗試類似的東西時,它就像魅力一樣。

任何想法?我已經通過了幾次文檔,但根本看不到我要去哪裏錯了。

回答

0

我找到了一個不同的方法來做到這一點。我沒有使用simple_form的標準關聯方法,而是簡單地將一個集合添加到Select Dropdown中,如下所示:

.row.padding-12-all 
    = builder.input :title_id, 
        :label_method => :name , 
        :value_method => :id, 
        :collection => Title.all