views:

12

answers:

1

I have a nested form that instantiates as this :

- 2.times { @organization.referrals.build }
- form_for @organization do |f|
  = f.error_messages
  - f.fields_for :referrals do |f|

Except, the nested forms are supposed to be always new and unique. Where as this form shows previously created objects as well.

So I tried to write as so..

- 2.times { @organization.referrals.build }
- form_for @organization do |f|
  = f.error_messages
  - f.fields_for @organization.referrals.select{|r| r.new_record? } do |f|

But now I do not see 2 blank forms, and I can not save my object because it tries to pass Organization.referral (which is not a method) instead of Organization.referrals .

Question 1

How do I create 2 blank forms?

Question 2

How do I get this to pass correctly ( as it does in the first example ) ?

+1  A: 

Try this:

model

class Organization < ActiveRecord::Base
  has_many :referrals
  accepts_nested_attributes_for :referrals
end

view

<%= form_for @organization do |f| %>
  <% 2.times do |i| %>
    <%= f.fields_for :referrals, @organization.referrals.build, :index => i do |rf| %>
       <%= rf.text_field :some_referral_attribute %>
    <% end %>
  <% end %>
<% end %>
jenjenut233
Sorry i marked your answer correct thinking it was theoretically accurate, but in actuality, no fields get generated when I run this.
Trip