ブロックは関係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 %>
また、コントローラーと a の新しい@parent
オブジェクトを試してみました@parent.build_child
(assoc を a に変更しましたhas_one
)。それでも同じ結果が見られました。