我正在努力寻找实施解决方案的最佳方式,我希望听到您对此的看法。
我有一个包含多个字段的表单(rails - simpleform)。其中一些字段具有关联的复选框。复选框的使用很简单:如果用户想要将该字段留空(数据可能丢失),则需要选中该复选框,这是为了确保他将该字段留空为目的。
现在我正在使用javascript和jquery验证来控制复选框。对于我在模型上使用的字段,进行以下验证
validates :field, acceptance: true, :unless => :field?
这是有效的,除非用户忘记输入一些数据,并且rails会抛出一些错误(例如:“必须接受”字段),然后页面刷新并且复选框状态丢失。
要在“编辑”或“更新”操作中控制此操作,我有一个在文档加载上运行的脚本,用于检查字段的内容是否为空,如果是,则表明应该选中该复选框。 ..
除了效率不高之外,当发生错误并刷新页面时,它不会对“新”或“创建”操作起作用。
所以我正在寻找更好的解决方案......我应该使用类似jquery.cookie的东西并将复选框状态存储在cookie上吗?我应该将所有复选框添加到数据库并以这种方式控制状态吗? (我不喜欢使用巨大的表来控制数据库中的所有内容...)我应该将状态作为参数传递给表单吗?
您对实现此目标的最佳解决方案有何看法?
---更新---
这是控制器代码:
respond_to do |format|
if @sample.save
format.html { redirect_to @sample, notice: 'Sample was successfully created.' }
format.json { render json: @sample, status: :created, location: @sample }
else
format.html { render action: "new" }
format.json { render json: @sample.errors, status: :unprocessable_entity }
end
end
并且在观点上:
<% if @sample.errors.any? %>
<div id="error_explanation" class="field_with_errors">
<h3>Não foi possível gravar o registo. Erro(s) encontrado(s): <%= pluralize(@sample.errors.count, "erro") %></h3>
<ul>
<% @sample.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
表格
<%= simple_form_for @sample, :html => { :class => 'form-horizontal' } do |f| %>
<!-- Error messages while saving-->
<% if @sample.errors.any? %>
<div id="error_explanation" class="field_with_errors">
<h3>Não foi possível gravar o registo. Erro(s) encontrado(s): <%= pluralize(@sample.errors.count, "erro") %></h3>
<ul>
<% @sample.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<br>
<!-- end of error messages -->
<div class="row">
<div class="span4">
<fieldset>
<legend>Detalhes da Informação</legend>
<div class="control-group">
<%= f.label :pcris, "PCR Anterior?", :class => 'control-label' %>
<div class="controls">
<%= f.select :pcris, NAOPREENCHIDO, :include_blank => true %>
</div>
</div>
<div class="resultpcr control-group">
<%#= f.label :resultFirstTest, "Res. PCR Anterior", :class => 'control-label' %>
<div class="controls">
<%#= f.select :resultFirstTest, RESULTADO, :include_blank => true, :id => 'result_first_test' %>
</div>
</div>
<div class="control-group">
<%= f.label :colheitaPCR, "Colheita de PCR", :class => 'control-label' %>
<div class="controls">
<%= f.select :colheitaPCR, COLHEITA_PCR, :include_blank => true %>
</div>
</div>
<div class="control-group">
<%= f.label :dateOfSample, "Data de Colheita", :class => 'control-label' %>
<div class="controls">
<%= f.text_field :dateOfSample, :class => 'date_field' %><input type="checkbox" name="dtc" id="dtc_nao_preenchido">nao preenchido<br>
</div>
</div>
<div class="control-group">
<%= f.label :horaColheita, "Hora de Colheita", :class => 'control-label' %>
<div class="controls">
<%= f.text_field :horaColheita, :class => 'time_field' %><input type="checkbox" name="hc" id="hc_nao_preenchido">nao preenchido<br>
</div>
</div>
<div class="control-group">
<%= f.label :dateSampleSent, "Data de Envio da U.S.", :class => 'control-label' %>
<div class="controls">
<%= f.text_field :dateSampleSent, :class => 'date_field' %><input type="checkbox" name="dtss" id="dtss_nao_preenchido">nao preenchido<br>
</div>
</div>
<div class="dateSampleReceived control-group">
<%= f.label :dateSampleReceived, "Data de Entrada no Lab", :class => 'control-label' %>
<div class="controls">
<%= f.text_field :dateSampleReceived, :class => 'date_field' %><input type="checkbox" name="dtsr" id="dtsr_nao_preenchido">nao preenchido<br>
</div>
</div>
<div class="control-group">
<%= f.label :sampleReceivedBy, "Amostra recebida por", :class => 'control-label' %>
<div class="controls">
<%= f.text_field :sampleReceivedBy, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<%= f.label :motherPMTCT, "PTV da Mãe", :class => 'control-label' %>
<div class="controls">
<%= f.select :motherPMTCT, MOTHER_PMTCT, :include_blank => true %>
</div>
</div>
<div class="control-group">
<%= f.label :infantPMTCT, "PTV da Criança", :class => 'control-label' %>
<div class="controls">
<%= f.select :infantPMTCT, INFANT_PMTCT, :include_blank => true %>
</div>
</div>
<div class="control-group">
<%= f.label :semanasPMTCT, "Quantas Semanas", :class => 'control-label' %>
<div class="controls">
<%= f.select :semanasPMTCT, SEMANAS_PMTCT, :include_blank => true %>
</div>
</div>
</fieldset>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-eid">
<i class="fa fa-check"> Gravar FSR</i>
</button>
<%= link_to '<i class="fa fa-times"> Cancelar</i>'.html_safe,
samples_path, :class => 'btn btn-danger' %>
</div>
<% end %>