Ruby:UnknownAttributeError

时间:2010-03-30 11:43:35

标签: ruby-on-rails

我有一些订单可以有几个项目,这些项目有相关的种类。善良可以属于许多物品。但是当我点击提交表单按钮时,我的OrdersController中出现了“未知属性:种类”。我使用嵌套表格btw。

Order.rb

class Order < ActiveRecord::Base
  validates_presence_of :ref_nr, :total_price

  has_many :items, :dependent => :destroy
  has_many :kinds, :through => :items

  accepts_nested_attributes_for :items
  accepts_nested_attributes_for :kinds

  validates_associated :items
  validates_associated :kinds


end

item.rb的

class Item < ActiveRecord::Base
  belongs_to :order
  has_one :kind

  accepts_nested_attributes_for :kind

  validates_associated :kind
end

Kind.rb

class Kind < ActiveRecord::Base
 belongs_to :items
end

OrdersController.rb:创建

def create
    @order = Order.new(params[:order])
end

new.erb.html

<% form_for @order do |f| %>
  <%= f.error_messages %>

  <% f.fields_for :items do |builder| %>
    <table>
    <tr>
      <% builder.fields_for :kinds do |m| %>
        <td><%= m.collection_select :kind, Kind.find(:all, :order => "created_at DESC"), :id, :name, {:prompt   => "Select a Type" }, {:id => "selector", :onchange => "kind_change(this)"} %></td>
    <% end %>
      <td><%= builder.text_field :amount, :id => "amountField", :onchange => "change_total_price()" %></td>
      <td><%= builder.text_field :text, :id => "textField" %></td>
      <td><%= builder.text_field :price, :class => "priceField", :onChange => "change_total_price()" %></td>
      <td><%= link_to_remove_fields "Remove Item", f %></td>
    </tr>
    </table>
    <% end %>
    <p><%= link_to_add_fields "Add Item", f, :items %></p>
    <p>
      <%= f.label :total_price %><br />
      <%= f.text_field :total_price, :class => "priceField", :id => "totalPrice" %>
    </p>
    <p><%= submit_tag %></p>
<% end %>

我无法看到我遗失的内容

1 个答案:

答案 0 :(得分:2)

您应该在订单模型中删除accepts_nested_attributes_for :kinds,它应该在项目模型中(如代码中所示)。然后在这里更改视图:

...
<% f.fields_for :items do |builder| %>
  <table>
    <tr>
      <% builder.fields_for :kind do |m| %>
...

我认为你也错误地遵循以下几点:

<td><%= f.text_field :amount, :id => "amountField", :onchange => "change_total_price()" %></td>
<td><%= f.text_field :text, :id => "textField" %></td>
<td><%= f.text_field :price, :class => "priceField", :onChange => "change_total_price()" %></td>

如果字段amounttextprice与商品模型相关联,那么您应该使用builder代替f

<td><%= builder.text_field :amount, :id => "amountField", :onchange => "change_total_price()" %></td>
<td><%= builder.text_field :text, :id => "textField" %></td>
<td><%= builder.text_field :price, :class => "priceField", :onChange => "change_total_price()" %></td>

编辑(回答评论中的其他问题):

你应该:

<% f.fields_for :items do |builder| %>
  <table>
    <tr>
      <td><%= builder.collection_select :kind_id, Kind.find(:all, :order => "created_at DESC"), :id, :name, {:prompt   => "Select a Type" }, {:id => "selector", :onchange => "kind_change(this)"} %></td>  
      <td><%= builder.text_field :amount, :id => "amountField", :onchange => "change_total_price()" %></td>
      <td><%= builder.text_field :text, :id => "textField" %></td>
      <td><%= builder.text_field :price, :class => "priceField", :onChange => "change_total_price()" %></td>

依旧......