开发者

Missing block error on nested_form rails 3

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_b开发者_运维百科elongs_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 :)


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜