创建验证

时间:2013-08-13 17:47:28

标签: ruby-on-rails ruby

此时我有一个包含这些字段的新表单

    attr_accessible :username, 
                :email, 
                :password, 
                :password_confirmation, 
                :name,
                :identificacion,
                :genero,
                :acceso_remoto,
                :estado,
                :telefono,
                :celular,
                :contacto,
                :direccion,
                :eps,
                :pension,
                :arp,
                :extension,
                :fecha_nacimiento,
                :photo

我也对其中一些人进行了验证

validates_presence_of   :username, 
                        :email,
                        :genero,
                        :name,
                        :identificacion,
                        :acceso_remoto,
                        :estado, :message => "Dato Obligatorio"

事情是,当我试图保存时,我得到了这个

错误al Guardar los Cambios

Genero Dato Obligatorio
Acceso remoto Dato Obligatorio
Estado Dato Obligatorio

但我已经设置了信息,并在日志中得到了

Started POST "/users" for 127.0.0.1 at 2013-08-13 12:49:34 -0500
Processing by UsersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"0HOSpzX5GNVsISYGUfWcO6dreghWqw6LAurPw6xhq9Y=", "user"=>{"identificacion"=>"10375957665", "name"=>"Telred BSE", "fecha_nacimiento"=>"2013-08-14", "genero"=>"false", "direccion"=>"aaasd", "telefono"=>"6041057", "username"=>"andru1989", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "email"=>"andruvizcaisno1989@hotmail.com", "contacto"=>"DIDIER ALEXANDER HENAO ARANGO", "celular"=>"3104121262", "estado"=>"false", "acceso_remoto"=>"false", "extension"=>"56456", "eps"=>"eps de prueba", "pension"=>"eps", "arp"=>"eps"}, "commit"=>"Guardar Cambios"}

字段就是那样

                          <%= f.input :genero, 
                                :collection => {"Masculino" => true, "Femenino" => false}.sort,
                                :input_html => {:style => 'width: 120px;'},
                                :label      => false,
                                :error      => false %>

                            <div class="span2">
                            <%= f.input :estado, 
                                :collection => {"Activo" => true, "Inactivo" => false}.sort,
                                :input_html => {:style => 'width: 90px;'},
                                :label      => false,
                                :error      => false %>
                        </div>
                        <div class="span2">
                            <%= f.input :acceso_remoto, 
                                :collection => {"SI" => true, "NO" => false}.sort,
                                :input_html => {:style => 'width: 60px;'},
                                :label      => false,
                                :error      => false %>
                        </div>

然后我不知道是什么问题 对不起我的英文,非常感谢你的帮助

1 个答案:

答案 0 :(得分:4)

由于这些字段是布尔字段validates_presence_of将无法按预期工作。您想使用validates_inclusion_of :field_name, in: [true, false]

请参阅:http://apidock.com/rails/ActiveRecord/Validations/ClassMethods/validates_presence_of