0

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

    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"

问题是,当我尝试保存时,我得到了这个

Error 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>

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

4

1 回答 1

4

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

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

于 2013-08-13T17:54:22.593 回答