我不断在 PartyController#create 中收到 ActiveRecord::UnknownAttributeError:
我有三个模型:Party、Products、Traits。
class Party < ActiveRecord::Base
has_many :products
accepts_nested_attributes_for :products
end
class Product < ActiveRecord::Base
belongs_to :party
has_many :traits
accepts_nested_attributes_for :traits
end
class Trait < ActiveRecord::Base
belongs_to :product
end
聚会形式:
<%= form_for(@party) do |f| %>
<% if @party.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@party.errors.count, "error") %> prohibited this party from being
saved:</h2>
<ul>
<% @party.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<table>
<%= f.fields_for :products do |builder| %>
<tr>
<td> <%= builder.label :shirt, "Shirt " %> </td>
<td> <%= builder.text_field :shirt %> </td>
<%= f.fields_for :traits do |builder| %>
<td><%= builder.label :color, "Color" %></td>
<td><%= builder.text_field :color %></td>
<td><%= builder.label :quantity, "Quantity" %></td>
<td><%= builder.number_field :quantity %></td>
</tr>
<tr>
<td><%= builder.label :pants, "Pants " %></td>
<td><%= builder.text_field :pants %></td>
<td><%= builder.label :color, "Color" %></td>
<td><%= builder.text_field :color %></td>
<td><%= builder.label :quantity, "Quantity" %></td>
<td><%= builder.number_field :quantity %></td>
</tr>
<% end %>
<% end %>
</table>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
当事人控制人:
class PartiesController < ApplicationController
def new
@party = Party.new
1.times do
product = @party.products.build
2.times {product.traits.build}
end
end
def create
@party = Party.new(params[:party])
respond_to do |format|
if @party.save
format.html { redirect_to @party, notice: 'Party was successfully created.' }
format.json { render json: @party, status: :created, location: @party }
else
format.html { render action: "new" }
format.json { render json: @party.errors, status: :unprocessable_entity }
end
end
end
显示页面:<%= 通知 %>
This is <%= @party.name %>'s registry:
<table>
<% for product in @party.products %>
<tr>
<td>Shirt</td>
<td> <%= product.shirt %> </td>
<% for trait in @product.traits %>
<td>Color</td>
<td> <%= trait.color %> </td>
<td>Quantity</td>
<td> <%= trait.quantity %> </td>
</tr>
<tr>
<td>Pants</td>
<td> <%= product.pants %> </td>
<td>Color</td>
<td> <%= trait.color %> </td>
<td>Quantity</td>
<td> <%= trait.quantity %> </td>
</tr>
<% end %>
</table>
<%= link_to 'Edit', edit_party_path(@party) %> |
<%= link_to 'Back', parties_path %>
<% end %>
我什么都试过了。我错过了什么?感谢人们!