我正在为一家医院开发西班牙语系统。我有一个名为 persona 的脚手架。
好吧,在我的控制器中,我得到了一个“新”和“创建”方法,几乎总是如此。我可以进入 new_persona 视图(带有表单)并输入一些数据。现在的问题是,当我想创建我插入的人时,我收到以下错误:
“Hds::PersonasController#create 中的 ActiveRecord::StatementInvalid”
此外,它告诉我 2 列不能为空。当然,我得到了 mysql 选项 null 应该是 false,因为我不希望这 2 列永远是空的。问题是即使我在这些字段中插入了一些东西,rails 也会出现这个错误。希望你能理解我并能帮助我。这里的代码:
personas_controller 新建并创建:
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_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 %>
谢谢您的帮助!!问候,一氧化碳
这里是完整的错误信息:
Mysql2::Error: 列 'apellido_pat' 不能为空: 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
)值(NULL,NULL,NULL,NULL,NULL,NULL,NULL,'2013-06-24 07:39:24',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,空,空,空,空,空,空,空,空,空,空,空,空,空,空,空,空,空,空,空,空,空,空,'2013-06-24 07:39: 24')
这里的参数:
{"utf8"=>"✓", "authenticity_token"=>"VYHiP9/zLNjZ0ElAFh1IeC4s5X5oxVFFhpbmbK2oVAs=", "hds_persona"=>{"numero_doc"=>"", "sexo"=>"masculino", "nombres"=> “测试”、“apellido_pat”=>“测试”、“apellido_mat”=>“”、“方向”=>“”、“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"=>"创建角色"}