2012-05-05 14 views
0

fields_forブロックはhas_manyの関係で出力されません。この問題は、私が取り組んでいるやや複雑なプロジェクトで起きました。私はそれを非常に単純なテストケースに分割しましたが、それでも動作しません。この質問はこれまでに尋ねられており、問題は通常 であり、ネストされたオブジェクトは存在しませんでした。しかしここでは、コードコメントで説明したように、ネストされたオブジェクトが存在するように見えます。私はレールにはかなり新しいので、それは明らかなものかもしれません。ここfields_for has_manyは出力されません

は、単純なケース・モデル・コードである:

class Parent < ActiveRecord::Base 
    has_many :children 
    accepts_nested_attributes_for :children 
end 

class Child < ActiveRecord::Base 
    belongs_to :parent 
end 

単純なケースコントローラ:

class ParentController < ApplicationController 
    def index 
    @parent = Parent.find_by_id(1) 
    end 
end 

単純なケースビュー:

<%= form_for @parent, {:url=>{:action=>:index}} do |f| %> 

    <!-- this outputs ok --> 
    <%= f.text_field :name %> 

    <% f.object.children.each do |c| %> 

     <!-- this outputs "child1", so the nested object exists --> 
     <%= c.name %> 

     <% f.fields_for c do |field| %> 
      this line does NOT output, nor does the field below 
      <%= field.text_field :name %> 
     <% end %> 
    <% end %> 
<% end %> 

私もこれを試みたと同じ結果を見:

<%= form_for @parent, {:url=>{:action=>:index}} do |f| %> 
    <%= f.text_field :name %> 
    output here  
    <% f.fields_for :children do |field| %> 
     no output here nor the field below 
     <%= field.text_field :name %> 
    <% end %> 
<% end %> 

Iは、コントローラに新しい@parent対象としようと、@parent.build_childhas_oneに連想を変更しました)。それでも同じ結果が見られました。

答えて

1

=記号を<%の後に付けるのを忘れてしまった。

は交換してください:

<% f.fields_for :children do |field| %> 

で:

<%= f.fields_for :children do |field| %> 
+0

を解決します!あなたの時間の束に感謝しています。レガーズ – ddndentJohn

関連する問題