【问题标题】:Trouble on `validates_inclusion_of` using radio buttons使用单选按钮在“validates_inclusion_of”上遇到问题
【发布时间】:2011-07-01 01:03:23
【问题描述】:

我正在使用 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

我该如何解决这个问题?


我也尝试过使用(如页面上方注释中的here 所述)

%w(beautiful, ugly, good, bad)

validates_inclusion_of 格式

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

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

但我总是收到验证错误。

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 validation inclusion


    【解决方案1】:

    解决方案(dho!)

    我忘记设置属性attr_accessible

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-16
      • 2014-12-08
      • 1970-01-01
      • 1970-01-01
      • 2014-03-21
      • 1970-01-01
      • 2014-01-07
      • 1970-01-01
      相关资源
      最近更新 更多