Not sure what I am doing wrong here. But I am trying to create a form that contains multiple models. I have read up on it and it seems straight forward, but I am unable to get the fields of the child model to render on the form. As I understand the solution I have to put the following into the models
class Message < ActiveRecord::Base
#parent model
attr_accessible :duns, :ord
has_many :po_recommits
accepts_nested_attributes_for :po_recommits
end
class PoRecommit < ActiveRecord::Base
#child model
attr_accessible :comment, :detail_reason_code, :pid, :po, :qty,
belongs_to :message, :dependent => :destroy
end
and put the following into the parent controller to create place holders
def new
@message = Message.new
5.times { @message.po_recommits.build }
end
and finally add the fields_for helper to the view
<%= form_for @message do |f| %>
<%= f.label :org %><%= f.text_field :org %>
<% f.fields_for :po_recommits do |builder| %>
<%= builder.label :po %><%= builder.text_field :po %>
<% end %>
<%= f.submit %>
<% end %>
This code doesn’t throw any errors but does not render the :po fields on the form. I assume this is because the block is executing 0 times.
What am I missing?
I think
<% f.fields_for :po_recommits do |builder| %>
should be
<%= f.fields_for :po_recommits do |builder| %>