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