1

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

しかし、私は常に検証エラーを受け取ります。

4

1 に答える 1