0

我有一個教師檔案模型,它有許多主題(單獨的模型)。我想將主題添加到用於創建/編輯配置文件的相同表單上的配置文件。我使用的是accep_nested_attributes,這對創建工作很好。但是在編輯頁面上,我收到了一個非常奇怪的錯誤 - 我看到12個主題(!),而不是看到3個主題(我在創建時添加了3個主題,並且看到控制檯確認了這一點)。接受嵌套屬性 - 編輯表單顯示不正確的項目數(+!map:ActiveSupport :: OrderedHash {})

#Profile model 

class Profile < ActiveRecord::Base  

    has_many :subjects 
    accepts_nested_attributes_for :subjects 

end 

#Subject Model 

class Subject < ActiveRecord::Base 

belongs_to :profile 

end     

#Profile Controller (only showing deviations from normal RESTFUL setup) 

def new 
@profile = Profile.new 
    3.times do 
    @profile.subjects.build 
end 
end 


#Here's 1 of three parts of the subject output of = debug @profile 
    errors: !ruby/object:ActiveRecord::Errors 
     base: *id004 
     errors: !map:ActiveSupport::OrderedHash {} 

    subjects: 
    - &id001 !ruby/object:Subject 
     attributes: 
     exam: Either 
     name: "7" 
     created_at: 2010-04-15 10:38:13 
     updated_at: 2010-04-15 10:38:13 
     level: Either 
     id: "31" 
     profile_id: "3" 
     attributes_cache: {} 

# Note that 3 of these attributes are displayed despite me seeing 12 subjects on screen 

其他信息的情況下,這是相關的。

的Rails:2.3.5,1.8.7紅寶石P149,HAML,inherited_resources

我從來沒有過這麼大的困難與之前的錯誤 - 我已經失去了約8個小時吧。非常感謝任何幫助!

由於任何勇敢的考生

傑克

+0

請您發表您使用的'edit'的代碼? – 2010-04-15 11:28:43

+0

我使用inherited_resources來保持我的控制器很薄,所以它只是標準的編輯。要仔細檢查這是不是導致問題,我也重寫了一個標準的控制器無濟於事。 – 2010-04-16 12:12:04

回答

1

原來這是與編輯表單的問題。我不小心將嵌套字段塊設置爲(fields_for),因爲插入了ruby而不是,評估爲ruby

因此,而不是寫這個

 

    - form.fields_for :subjects do |ff| 
    = ff.collection_select :name, Subject.all, :id, :name, :include_blank => true 
    = ff.select :exam, ["Either", "Leaving Cert Only"] 
    = ff.select :level, ["Either", "Higher Level Only"]  
 

我寫了這個:

 

    = form.fields_for :subjects do |ff| 
    = ff.collection_select :name, Subject.all, :id, :name, :include_blank => true 
    = ff.select :exam, ["Either", "Leaving Cert Only"] 
    = ff.select :level, ["Either", "Higher Level Only"]