【发布时间】:2010-04-15 11:03:32
【问题描述】:
我有一个教师档案模型,其中包含许多科目(单独的模型)。我想在同一表单上将主题添加到配置文件以创建/编辑配置文件。我正在使用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 个小时。非常感谢任何帮助!
感谢所有勇敢的接受者
杰克
【问题讨论】:
-
能否将您使用的代码发布到
edit? -
我使用了inherited_resources 来保持我的控制器精简,所以它只是标准编辑。为了仔细检查这不是导致问题的原因,我还将控制器重写为标准控制器,但无济于事。
标签: ruby-on-rails activerecord nested-forms nested-attributes