2012-08-08 29 views
0

我有兩個模型類,用於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 

+0

請附上您的'EmployeeDetailsController#create'方法的代碼。 – Thilo 2012-08-08 09:14:13

+0

可能是它對你有幫助http://stackoverflow.com/questions/4249856/activerecordassociationtypemismatch-in-commentscontrollercreate – 2012-08-08 10:22:43

回答

1

當你任何模型關聯,則應該使用模型的小寫版本名稱。

變化:

belongs_to :Designation 

到:

belongs_to :designation 
+0

通過這樣做,它會給錯誤的看法。 <%= f.text_field:Designation%>因爲視圖還具有:目標並且它是自動生成的。所以我必須改變意見也?? – 2012-08-08 09:50:04

+0

更改belongs_to後:指定 to belongs_to:指定,它仍顯示相同的錯誤。 – 2012-08-08 09:54:39

+0

我認爲你正在嘗試使用嵌套窗體,請參閱http://railscasts.com/episodes/196-nested-model-form第一部分,這將幫助你。我們不應該使用:指定。 – pdpMathi 2012-08-08 09:57:58