开发者

Uninitialized constant Item::Types

开发者 https://www.devze.com 2022-12-23 18:00 出处:网络
I get this uninitialized constant error when i submit my nested forms. order.rb class Order < ActiveRecord::Base

I get this uninitialized constant error when i submit my nested forms.

order.rb

class Order < ActiveRecord::Base
  has_many :items, :dependent => :destroy
  has_many :types, :through => :items

  accepts_nested_attributes_for :items
  accepts_nested_attributes_for :types

  validates_associated :items
  validates_associated :types
end

item.rb

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

  accepts_nested_attributes_for :types

  validates_associated :types
end

type.rb

class Type < ActiveRecord::Base
  belongs_to :items
  belongs_to :orders
end

new.erb.html

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

  <% f.fields_for :items do |builder| %>
    <table border="0">
      <th>Type</th>
      <th>Amount</th>
      <th>Text</th>
      <th>Price</th>
      <tr>
      <% f.fields_for :type do |m| %>
        <td> <%= m.collection_select  :type, Type.find(:all, :order => "created_at DESC"), :id, :name, {:prompt   => "Select a Type" }, {:id => "selector", :onchange => "type_change(this)"} %> </td>
      <% end %>
        <td> <%= f.text_field :amount, :id => "amountField", :onchange => "change_total_price()" %> </td>
        <td> <%= f.text_field :text, :id => "textField" %> </td>
        <td&开发者_如何转开发gt; <%= f.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><%= f.submit "Create"%></p>
<% end %>

<%= link_to 'Back', orders_path %>

create method in orders_controller.rb

def create
  @order = Order.new(params[:order])
  respond_to do |format|
    if @order.save
      flash[:notice] = 'Post was successfully created.'
      format.html { redirect_to(@order) }
      format.xml  { render :xml => @order, :status => :created,
                  :location => @order }
    else
      format.html { render :action => "new" }
      format.xml  { render :xml => @order.errors,
                  :status => :unprocessable_entity }
    end
  end
end

Hopefully you can see what i cant


You need to pay careful attention to pluralization in Rails. In this case you're setting up a singular relationship to a plural thing, so it's presumed that you're actually calling a class named "Types" and not "Type".

  • has_one, belongs_to are singular
  • has_many is plural

Possible fixes:

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

  accepts_nested_attributes_for :type

  validates_associated :type
end

class Type < ActiveRecord::Base
  belongs_to :item
  belongs_to :order
end


In rails, type is a reserved word. You have to rename your model to something else. Also you have to follow tadman's instruction about singular names for has_one association.

Reference

Reserved words in rails

0

精彩评论

暂无评论...
验证码 换一张
取 消