【发布时间】:2013-03-17 03:46:38
【问题描述】:
我这几天一直在努力解决这个问题。我不断收到错误:
ActiveRecord::AssociationTypeMismatch in SatellitesController#create
Satellite(#70098878574220) expected, got String(#70098849353340)
我浏览了整个网站,但似乎没有任何帮助。到目前为止,这是我的代码中的内容:
在我的 new.html.erb 文件中:
<%= form_for( @satellite ) do |f| %>
<div class="field">
<%= f.label :parent_id %></br>
<%= f.select( :parent_id, Satellite.all.collect { |s| [ s.name, s.id ] }, { :include_blank => '-select-' } ) %>
</div>
这是我正在使用的关联:
class Satellite < ActiveRecord::Base
validates :name, :presence => true, :uniqueness => true
has_many :satellites, class_name: 'Satellite', foreign_key: 'parent_id'
belongs_to :parent_id, class_name: 'Satellite', foreign_key: 'parent_id'
end
任何帮助将不胜感激!
【问题讨论】:
-
显示实际错误的堆栈跟踪以及它在您的问题中引用的代码行。
-
是的,特别是您的
create方法在您的SatellitesController。 -
def create @satellite = Satellite.new(params[:satellite]) if @satellite.save redirect_to @satellite,注意:“卫星已成功创建。”否则渲染动作:“新”结束结束
标签: ruby-on-rails forms select