Note that there are some explanatory texts on larger screens.

plurals
  1. POformtastic - subset of values in accepts_nested_attributes_for
    primarykey
    data
    text
    <p>I have a model:</p> <pre><code>class Contact &lt; ActiveRecord::Base has_many :phones accepts_nested_attributes_for :phones end </code></pre> <p>I want to build 50 phone #s that users can add (there may already be phones 1 or 5, but I always want 50 available) In my controller:</p> <pre><code>while contact.phones.length &lt; 50 contact.phones.build({:phone_type_id =&gt; PhoneType['a_cool_type'].id}) end </code></pre> <p>In my view, I want to have 2 columns of phone #s 25 rows each</p> <pre><code> &lt;%= semantic_form_for contact do |form| %&gt; &lt;table width=50%&gt; &lt;%= form.inputs :for =&gt; :phones[0..25] do |phone_form| %&gt; &lt;td align="center"&gt;&lt;%= phone_form.input :number, :label =&gt; false %&gt;&lt;/td&gt; .... &lt;% end %&gt; &lt;/table&gt; &lt;table width=50%&gt; &lt;%= form.inputs :for =&gt; :phones[25..49] do |phone_form| %&gt; &lt;td align="center"&gt;&lt;%= phone_form.input :number, :label =&gt; false %&gt;&lt;/td&gt; .... &lt;% end %&gt; &lt;/table&gt; &lt;%end %&gt; </code></pre> <p>Obviously the line:</p> <pre><code>&lt;%= form.inputs :for =&gt; :phones[25..49] do |phone_form| %&gt; </code></pre> <p>doesn't work, but it conveys my intention ( I hope). I want to have more control over how formtastic grabs the underlying object association.</p> <p>The following works, but I can't do two columns easily without fancy css.</p> <pre><code>&lt;%= form.inputs :for =&gt; :phones do |phone_form| %&gt; </code></pre> <p>Any suggestions?</p> <p>---------- Update ---- </p> <p>I was able to get around this in a roundabout way: I built up a separate list of phone #s not as contact.phones.build, but Phone.new(:contact_id => contact.id) and store those in a list called @new_phones</p> <p>Then my form looks like this:</p> <pre><code>&lt;%= semantic_form_for @contact, :url =&gt; ..., do |f| %&gt; &lt;% @new_phones[0...25].each_with_index do |phone, i| %&gt; &lt;%= f.fields_for :phones, phone, :child_index =&gt; i do |phone_form| %&gt; &lt;%= render "phone_fields", {:phone_form =&gt; phone_form, :phone =&gt; phone} %&gt; &lt;%end%&gt; &lt;% end %&gt; .... &lt;% @new_phones[25...50].each_with_index do |phone, i| %&gt; &lt;%= f.fields_for :phones, phone, :child_index =&gt; i+25 do |phone_form| %&gt; &lt;%= render "phone_fields", {:phone_form =&gt; phone_form, :phone =&gt; phone} %&gt; &lt;%end%&gt; &lt;% end %&gt; &lt;%end%&gt; </code></pre> <p>This allowed me to display 25 phones on one part of the page, and 25 on another, with nested_attributes_for :phones working as expected on form submit. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload