节点预期,得到String

时间:2011-04-18 02:57:40

标签: ruby-on-rails

我正在尝试构建一个树结构,因此我创建了一个带有自连接关联的Node模型:

class Node < ActiveRecord::Base
  has_many :child_nodes, :class_name => "Node", :foreign_key => "parent_id"
  belongs_to :parent, :class_name => "Node"

这是架构:

create_table "nodes", :force => true do |t|
  t.integer  "key"
  t.decimal  "value",      :precision => 8, :scale => 2
  t.decimal  "prob",       :precision => 8, :scale => 2
  t.integer  "parent_id"
  t.datetime "created_at"
  t.datetime "updated_at"
end

当我运行测试或通过控制台创建节点时,一切似乎都运行良好。但是,当我运行应用程序并通过/ nodes / new上的表单创建一个新节点时,我收到以下错误:

ActiveRecord::AssociationTypeMismatch in NodesController#create

Node(#2162787060) expected, got String(#2151988680)

什么似乎是问题?

编辑:添加了表单和create

<%= form_for(@node) do |f| %>
  <% if @node.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@node.errors.count, "error") %> prohibited this node from being saved:</h2>

      <ul>
      <% @node.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :key %><br />
    <%= f.number_field :key %>
  </div>
  <div class="field">
    <%= f.label :parent %><br />
    <%= f.number_field :parent %>
  </div>
  <div class="field">
    <%= f.label :value %><br />
    <%= f.number_field :value %>
  </div>
  <div class="field">
    <%= f.label :prob %><br />
    <%= f.number_field :prob %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

创建:

def create
  @node = Node.new(params[:node])

  respond_to do |format|
    if @node.save
      format.html { redirect_to(@node, :notice => 'Node was successfully created.') }
      format.xml  { render :xml => @node, :status => :created, :location => @node }
    else
      format.html { render :action => "new" }
      format.xml  { render :xml => @node.errors, :status => :unprocessable_entity }
    end
  end
end

0 个答案:

没有答案