接受嵌套属性 - 编辑表单显示不正确的项目数量 ( + !map:ActiveSupport::OrderedHash {} )
我有一个教师档案模型,其中有很多科目(单独的模型)。我想在创建/编辑个人资料的同一表单上将主题添加到个人资料中。我正在使用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
我以前从未在 bug 上遇到过如此大的困难 - 我已经为此损失了大约 8 个小时。非常感谢任何帮助!
感谢所有勇敢的接受者
杰克
I have a teacher profile model which has many subjects (separate model). I want to add subjects to the profile on the same form for creating/editing a profile. I'm using accepts_nested_attributes for and this works fine for creation. However on the edit page I am getting a very strange error - instead of seeing 3 subjects (I added three at create and a look into the console confirms this), I see 12 subjects(!).
#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
Other info in case it's relevant.
Rails: 2.3.5, Ruby 1.8.7 p149, HAML, inherited_resources
I've never had so much difficulty with a bug before - I've already lost about 8 hours to it. Would really appreciate any help!
Thanks to any courageous takers
Jack
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
原来是编辑表单的问题。我不小心将嵌套字段块设置为 (fields_for) 作为 插入 ruby 而不是 评估 ruby 。
因此,我没有写这个,而是
写了这个:
Turns out it was an issue with the edit form. I had accidentally set the nested fields block as (fields_for) as inserted ruby rather than evaluated ruby .
Thus instead of writing this
I wrote this: