2011-08-29 81 views
1

我有我的模型torubles。 我的代碼:Rails模型驗證 - validates_inclusion_of

class Typeofaid < ActiveRecord::Base 
    validates_inclusion_of :type, :in => %w(Food Water Health) 
end 

但每當我在框中鍵入「食品」等我拿到

`1 error prohibited this typeofaid from being saved: 

    * Type is not included in the list 
` 

誰能幫助?

回答

3

我認爲你必須使用type屬性來實現你的模型,而不是僅僅使用輸入框輸入一些值。

「type」是Rails中單表繼承的保留列。

+0

做工精細,我有一個在這個問題:http://stackoverflow.com/q/435678/860536 – flooooo

+0

那麼究竟是什麼我必須改變嗎? – Sean

+0

食物,水,健康孩子類的「typeofaid」或只是字符串值?如果它們是字符串值,只需將屬性名稱從類型更改爲不同的名稱 - 可能是名稱。 – flooooo

2

將其更改爲下面的代碼,並嘗試爲它在Rails3中

validates :type, :inclusion => {:in => %w(Food Water Health)}