ActiveRecord的错误
我有两个模型类,用于EmployeeDetailActiveRecord的错误
class Designation < ActiveRecord::Base
attr_accessible :name
validates :name, presence: true, uniqueness: { case_sensitive: false }
has_many :employee_details
accepts_nested_attributes_for :employee_details
end
和
class EmployeeDetail < ActiveRecord::Base
belongs_to :Designation
attr_accessible :bloodGroup, :dob, :doc, :doj, :empId, :name, :panNo, :status, :Designation
end
我已经生成的默认支架。从EmployeeDetail页面,当我尝试创建并在指定文本框中输入整数值时,它给我错误 ActiveRecord::AssociationTypeMismatch in EmployeeDetailsController#create Designation(#81846160) expected, got String(#75419260)
。
任何人都可以帮我理清这个问题吗? EmployeeDetailController#创建: - DEF创建 @employee_detail = EmployeeDetail.new(PARAMS [:employee_detail])
respond_to do |format|
if @employee_detail.save
format.html { redirect_to @employee_detail, notice: 'Employee detail was successfully created.' }
format.json { render json: @employee_detail, status: :created, location: @employee_detail }
else
format.html { render action: "new" }
format.json { render json: @employee_detail.errors, status: :unprocessable_entity }
end
end
末
当你任何模型关联,则应该使用模型的小写版本名称。
变化:
belongs_to :Designation
到:
belongs_to :designation
通过这样做,它会给错误的看法。 因为视图还具有:目标并且它是自动生成的。所以我必须改变意见也?? – 2012-08-08 09:50:04
更改belongs_to后:指定 to belongs_to:指定,它仍显示相同的错误。 – 2012-08-08 09:54:39
我认为你正在尝试使用嵌套窗体,请参阅http://railscasts.com/episodes/196-nested-model-form第一部分,这将帮助你。我们不应该使用:指定。 – pdpMathi 2012-08-08 09:57:58
请附上您的'EmployeeDetailsController#create'方法的代码。 – Thilo 2012-08-08 09:14:13
可能是它对你有帮助http://stackoverflow.com/questions/4249856/activerecordassociationtypemismatch-in-commentscontrollercreate – 2012-08-08 10:22:43