nested_form Rails 3 上缺少块错误

发布于 2024-12-03 07:28:21 字数 1114 浏览 2 评论 0原文

我有一些模型,比如

class CompanyDepartment < ActiveRecord::Base    
  belongs_to :company

  accepts_nested_attributes_for :phones, :allow_destroy => true
  attr_accessible :phones_attributes
end

class Phone < ActiveRecord::Base  
  has_and_belongs_to_many :companies
  has_and_belongs_to_many :company_departments 
end

我正在使用 nested_form ryanb gem。 当我创建 company_department 时,一切正常。 所有手机添加部分效果也很好。

但是当我编辑某些company_department时,出现错误 在手机上添加。

这是我的手机视图:

#views/company_departments/_tab_contacts.html.haml
%td
      #phones
        = f.fields_for :phones
        = f.link_to_add "add phone", :phones


#views/company_departments/_phone_fields.html.haml
= f.text_field :number
= f.link_to_remove "delete"

错误听起来像

 Missing block

 Extracted source (around line #7):

     #views/company_departments/_tab_contacts.html.haml 
     ...
     7:         = f.fields_for :phones
     ...

所以我通过 ajax 渲染此选项卡。 当我在没有 ajax 的情况下渲染时,不会显示错误,并且一切正常。 但我需要使用ajax:)

I have some models like

class CompanyDepartment < ActiveRecord::Base    
  belongs_to :company

  accepts_nested_attributes_for :phones, :allow_destroy => true
  attr_accessible :phones_attributes
end

class Phone < ActiveRecord::Base  
  has_and_belongs_to_many :companies
  has_and_belongs_to_many :company_departments 
end

I'm using nested_form ryanb gem.
All works good when I create new company_department.
All phone adding partial works good too.

But when I'm edit some company_department, I have error
on phones adding.

It is my phone views:

#views/company_departments/_tab_contacts.html.haml
%td
      #phones
        = f.fields_for :phones
        = f.link_to_add "add phone", :phones


#views/company_departments/_phone_fields.html.haml
= f.text_field :number
= f.link_to_remove "delete"

Error sounds like

 Missing block

 Extracted source (around line #7):

     #views/company_departments/_tab_contacts.html.haml 
     ...
     7:         = f.fields_for :phones
     ...

So i'm rendering this tab through ajax.
When i'm rendering without ajax error not shows and all works nice.
But i need to work with ajax :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

爱人如己 2024-12-10 07:28:21

确保在模型中添加一行具有适当的 accepts\_nested\_attributes\_for。这就是为我解决这个问题的原因。

Make sure to add a line in your model with the appropriate accepts\_nested\_attributes\_for. That's what fixed this for me.

月野兔 2024-12-10 07:28:21

错误消息表明您缺少 fields_for 方法的块。
代码可能应该如下所示:

#phones
  = f.fields_for :phones do |p|
    = p.link_to_add "add phone", :phones

如果您查看字段的文档示例,你会看到那个块。
您还将在
nested_form 自述文件中看到该块语法。

The error message says that you're missing a block for the fields_for method.
The code should probably look something like this:

#phones
  = f.fields_for :phones do |p|
    = p.link_to_add "add phone", :phones

If you look at the documentation examples for fields for, you'll see that block.
You'll also see that block syntax in the nested_form readme.

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