Ruby on Rails 3.0.7を使用していますが、この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
どうすれば問題を解決できますか?
私も使用しようとしました(ページの上のメモでここに説明されているように)
%w(beautiful, ugly, good, bad)
とvalidates_inclusion_of
フォーマット
validates_inclusion_of :test, :in => User::INCLUSION_VALUES
また
validates :test, :inclusion => User::INCLUSION_VALUES
しかし、私は常に検証エラーを受け取ります。