我在PartiesController #create:
中不断收到ActiveRecord :: UnknownAttributeError我有三种模式:派对,产品,特质。
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
展会页面: &lt;%= notice%&gt;
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 %>
我已经尝试了一切。我错过了什么?谢谢大家!
答案 0 :(得分:4)
因此params [:party]哈希可能包含表单中不属于Party属性的元素。要进行调试,请尝试:
def create
@party = Party.new(:name => params[:party][:name])
我认为真正的复杂因素是Traits嵌套在Party下嵌套的Products下。这就是为什么Traits的fields_for
方法必须是`builder.fields_for:traits do | traits_fields |' (虽然我认为你可以把“建造者”而不是“traits_fields”。
然后,对于所有特征字段,请使用<%= traits_fields.label :pants, "Pants " %>
然后它应该按照你的方式工作:
def create
@party = Party.new(params[:party])
答案 1 :(得分:2)
参数traits
中的错误,应该是"products_attributes"=>{"traits_attributes" => ...
。
试试这个:<%= builder.fields_for :traits do |traits_fields| %>
代替:<%= f.fields_for :traits do |builder| %>