1

我不断在 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 %>

我什么都试过了。我错过了什么?感谢人们!

4

2 回答 2

4

所以 params[:party] 散列可能包含表单中不是 Party 属性的元素。要调试,请尝试:

def create
  @party = Party.new(:name => params[:party][:name])

我认为真正的复杂之处在于 Traits 嵌套在 Products 下,而 Products 嵌套在 Party 下。这就是为什么fields_forTraits 的方法需要是 `builder.fields_for :traits do |traits_fields|' (尽管我认为您可以使用“builder”而不是“traits_fields”。

然后对于所有特征字段,使用<%= traits_fields.label :pants, "Pants " %>

然后它应该按照您的方式工作:

def create
  @party = Party.new(params[:party])
于 2012-05-22T15:02:20.383 回答
2

参数中的错误,traits应该是什么时候"products_attributes"=>{"traits_attributes" => ...

试试这个:<%= builder.fields_for :traits do |traits_fields| %>而不是:<%= f.fields_for :traits do |builder| %>

于 2012-05-22T15:35:02.693 回答