接受嵌套属性 - 编辑表单显示不正确的项目数量 ( + !map:ActiveSupport::OrderedHash {} )

发布于 2024-08-29 04:05:13 字数 1334 浏览 5 评论 0原文

我有一个教师档案模型,其中有很多科目(单独的模型)。我想在创建/编辑个人资料的同一表单上将主题添加到个人资料中。我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半寸时光 2024-09-05 04:05:13

原来是编辑表单的问题。我不小心将嵌套字段块设置为 (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"]

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


   - 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"]     

I wrote this:


   = 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"]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文