i'm working on a spanish system for a hospital. I got a scaffold named persona.
Well in my controller i got a "new" and a "create" method, like almost always. I can get to the new_persona view (with form) and enter some datas. The problem is now, when i want to create the person i've inserted, i get the following error:
"ActiveRecord::StatementInvalid in Hds::PersonasController#create"
Furthermore it says me that 2 columns cant be null. Sure, i got the mysql-option that null should be false, because i dont want this 2 columns ever to be empty. The problem is that rails rises this error even when i inserted something in those fields. Hope you understand me and can help me. Here the code:
personas_controller new and create:
def new
@persona = Hds::Persona.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @persona }
end
end
def create
@persona = Hds::Persona.new(params[:persona])
respond_to do |format|
if @persona.save!
format.html { redirect_to @persona, notice: 'Persona was successfully created.' }
format.json { render json: @persona, status: :created, location: @persona }
else
format.html { render action: "new" }
format.json { render json: @persona.errors, status: :unprocessable_entity }
end
end
end
form:
<%= form_for(@persona, :validate=>true) do |f| %>
<% if @persona.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@persona.errors.count, "error") %> prohibited this persona from being saved:</h2>
<ul>
<% @persona.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<table class="form_table">
<tr class="partial_head">
<th colspan="2">
<h3>Datos personales</h3>
</th>
</tr>
<tr>
<div class="field">
<td><%= f.label :numero_doc %></td>
<td><%= f.text_field :numero_doc %></td>
</div>
</tr>
[...]
</table>
f.submit
<% end %>
Thanks for the help!! Greetings, CO
Here the full error message:
Mysql2::Error: Column 'apellido_pat' cannot be null: INSERT INTO
hds_personas
(apellido_mat
,apellido_pat
,centro_trabajo
,ciudadania
,ciudadania2_id
,ciudadania_id
,conctacto
,created_at
,direccion
,documento_ident_id
,estado_cd
,estado_civil_cd
,estudio_id
,fecha_defuncion
,fecha_nacimiento
,nombre_comercial
,nombres
,nro_hijos
,numero_doc
,ocupacion
,ocupacion_id
,origen_etnico_cd
,profesion
,profesion_id
,razon_social
,religion_cd
,religion_string
,representante
,ruc
,servicio_basico_gral_id
,sexo_cd
,telf_contacto
,telf_fijo
,telf_movil
,tipo_documento_cd
,ubigeo_direccion_id
,ubigeo_nacimiento_id
,updated_at
) VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL, '2013-06-24 07:39:24', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '2013-06-24 07:39:24')
And here the parameters:
{"utf8"=>"✓", "authenticity_token"=>"VYHiP9/zLNjZ0ElAFh1IeC4s5X5oxVFFhpbmbK2oVAs=", "hds_persona"=>{"numero_doc"=>"", "sexo"=>"masculino", "nombres"=>"test", "apellido_pat"=>"test", "apellido_mat"=>"", "direccion"=>"", "fecha_nacimiento"=>"", "estado_civil"=>"soltero", "nro_hijos"=>"", "telf_movil"=>"", "email"=>"", "ubigeo_direccion_id"=>"", "ubigeo_nacimiento_id"=>"", "ciudadania_id"=>"", "ciudadania2_id"=>"", "origen_etnico"=>"asiatico", "religion"=>"catolico", "estudio_id"=>"", "ocupacion_id"=>"", "profesion_id"=>""}, "commit"=>"Create Persona"}
I got it, it should be
params[:hds_persona]
in my create-method.
Thank you!