Note that there are some explanatory texts on larger screens.

plurals
  1. POnested object forms
    primarykey
    data
    text
    <p>I'm a rails and ruby noob, and Im pretty sure this something completely stupid I am missing.. </p> <p>Trying to build a nested form, and have found many examples online, and tried and failed to reproduce the desired result.</p> <p>the "seller" fields are not even displayed, and when I submit form I get: </p> <blockquote> <p>unknown attribute: item</p> </blockquote> <p>models:</p> <pre><code>class Dealerform &lt; ActiveRecord::Base belongs_to :dealer has_one :seller :class_name =&gt; 'Seller' has_many :items, :through =&gt; :seller accepts_nested_attributes_for :seller, :items end class Seller &lt; ActiveRecord::Base belongs_to :dealerform has_many :items :class_name =&gt; 'Item' end class Item &lt; ActiveRecord::Base belongs_to :seller end </code></pre> <p>view:</p> <pre><code>&lt;% form_for(@dealerform) do |f| %&gt; &lt;%= f.error_messages %&gt; &lt;p&gt; &lt;%= f.label :date %&gt;&lt;br /&gt; &lt;%= f.datetime_select :date %&gt; &lt;/p&gt; #...more fields ... &lt;% f.fields_for :seller do |seller| %&gt; &lt;p&gt; &lt;%= seller.label :fname %&gt;&lt;br /&gt; &lt;%= seller.text_field :fname %&gt; &lt;/p&gt; #...more fields ... &lt;% end %&gt; &lt;% f.fields_for :item do |item| %&gt; &lt;p&gt; &lt;%= item.label :foo %&gt;&lt;br /&gt; &lt;%= item.text_field :foo %&gt; &lt;/p&gt; #...more fields ... &lt;% end %&gt; &lt;%= f.submit 'Create' %&gt; &lt;% end %&gt; </code></pre> <p>Based on suggestions.. changed the following lines:</p> <pre><code>&lt;% f.fields_for :seller do |seller| %&gt; &lt;% f.fields_for :item do |item| %&gt; </code></pre> <p>to:</p> <pre><code>&lt;% f.fields_for @seller do |seller| %&gt; &lt;% f.fields_for @item do |item| %&gt; </code></pre> <p>and now they show up in the form, and when trying to submit form I get:</p> <blockquote> <p>unknown attribute: nil_class</p> </blockquote> <p>when I change:</p> <pre><code>def new @dealerform = Dealerform.new </code></pre> <p>to:</p> <pre><code>def new @dealerform = Dealerform.new @dealerform.seller.build </code></pre> <p>I get:</p> <blockquote> <p>undefined method `build' for nil:NilClass</p> </blockquote>
    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