nested_form Rails 3 上缺少块错误
我有一些模型,比如
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保在模型中添加一行具有适当的
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.错误消息表明您缺少
fields_for
方法的块。代码可能应该如下所示:
如果您查看字段的文档示例,你会看到那个块。
您还将在 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:
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.