2011-07-01 31 views
1

我正在使用Ruby on Rails 3.0.7,並且在使用validates_inclusion_of方法時遇到問題。使用單選按鈕在`validates_inclusion_of`上遇到問題

我在我的模型文件我有:

class User < ActiveRecord::Base 
    INCLUSION_VALUES = ['beautiful', 'ugly', 'good', 'bad'] 

    validates :test, 
    :inclusion => { 
     :in => User::INCLUSION_VALUES 
    } 
end 

在我看來,文件我有

<%= form_for(@user) do |f| %> 
    <% User::INCLUSION_VALUES.each do |test| %> 
    <%= f.radio_button :test, test %> 
    <% end %> 
<% end %> 

上述 「意見」 的代碼生成此:

<input type="radio" value="beautiful" name="user[test]" id="user_test_beautiful"> 
<input type="radio" value="ugly" name="user[test]" id="user_test_ugly"> 
<input type="radio" value="good" name="user[test]" id="user_test_good"> 
<input type="radio" value="bad" name="user[test]" id="user_test_bad"> 

無論我做什麼提交給我的驗證錯誤的表單:

Test is not included in the list 

我該如何解決問題?


我還tryed使用(作爲網頁上面的說明中描述here

%w(beautiful, ugly, good, bad) 

validates_inclusion_of格式

validates_inclusion_of :test, :in => User::INCLUSION_VALUES 

validates :test, :inclusion => User::INCLUSION_VALUES 

但我總是得到驗證錯誤。

回答

2

SOLUTION(DHO!)

我忘記將屬性attr_accessible