我有一个教师档案模型,其中包含许多科目(单独的模型)。我想在同一表单上将主题添加到配置文件以创建/编辑配置文件。我正在使用accepts_nested_attributes,这对于创建来说很好。但是在编辑页面上我收到了一个非常奇怪的错误 - 而不是看到 3 个主题(我在创建时添加了三个,并且查看控制台确认了这一点),我看到了 12 个主题(!)。
#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,Ruby 1.8.7 p149,HAML,inherited_resources
我以前从来没有遇到过这么大的问题——我已经为此浪费了大约 8 个小时。非常感谢任何帮助!
感谢任何勇敢的接受者
杰克