我有以下控制器试图捕获异常:
def create
status = 'ok'
begin
# do something dodgy
@factura = Factura.execute_procedure :store_prueba, 1
flash[:notice] = "Éxito"
rescue ActiveRecord::StatementInvalid
flash[:notice] = nil
flash[:alert] = "Algo salio mal"
status = 'bad'
end
if status == 'ok'
render action: 'new'
elsif status == 'bad'
redirect_to action: :back
end
end
Flash消息显然有效,但是如果引发异常,则输入字段不会保持填充状态。
请记住我没有使用模型,我正在使用存储过程来执行此操作,因此这就是我没有@factura = Factura.new(factura_params)
或其他内容的原因。
这是视图:
<% if flash[:alert] %>
<div class="alert alert-danger alert-dismissible"><%= flash[:alert] %></div>
<% end %>
<% if flash[:notice] %>
<div class="alert alert-sucess alert-dismissible"><%= flash[:notice] %></div>
<% end %>
<%= form_tag({controller: "facturas", action: "create"}, method: :post ) do |f| %>
<!--<div class="form-inputs col-lg-6 panel panel-default col-lg-9" >-->
<div class="panel-body">
<div class="col-lg-4">
<%= label_tag "Folio:" %>
<%= text_field_tag :folio, nil, class: "form-control", required: "true" %>
</div>
<div class="col-lg-4">
<%= label_tag "Fecha:" %>
<%= date_field_tag :fecha, Date.current.to_default_s , class: "form-control", required: "true" %>
</div>
<div class="col-lg-6">
<%= label_tag "Almacen:" %>
<%= select_tag "almacen", "<option></option>".html_safe, class: "form-control col-lg-6", required: "true",
data: { placeholder: "Seleccione Almacen", toggle: "popover", placement: "top", content: "Debe seleccionar Almacen"} %>
</div>
<div class="col-lg-6">
<%= label_tag "Cliente:" %>
<%= select_tag "cliente", "<option></option>".html_safe, class: "form-control col-lg-6", required: "true",
data: { placeholder: "Seleccione un cliente", toggle: "popover", placement: "top", content: "Debe seleccionar Cliente" } %>
</div>
</div>
<div class="panel-content no-more-tables">
<table id="detalle" class="col-md-12 table table-bordered table-striped table-condensed no-more-tables cf">
<thead class="cf">
<tr>
<th>Artículo</th>
<th>Cantidad</th>
<th>Precio</th>
<th>Importe</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td data-title="Artículo">
<div class="input-group col-md-4">
<select onchange="obtener_ultimoprecio(this)" name="detalle_arreglo[][articulo]" data-placeholder="Seleccione un artículo" class="articulo form-control col-md-4" required="true"></select>
</div>
</td>
<td data-title="Cantidad">
<div class="input-group">
<span class="input-group-addon"></span>
<input class="form-control" type="number" aria-label="Cantidad" name="detalle_arreglo[][cantidad]" step="0.001" min="0.000" onchange="cambiar_importe(this)" required="true">
</div>
</td>
<td data-title="Precio" >
<div class="input-group">
<span class="input-group-addon">$</span>
<input type="number" class="form-control" aria-label="Precio" name="detalle_arreglo[][precio]" onchange="cambiar_importe(this)" step="0.001" min="0.000" required="true">
</div>
</td>
<td data-title="Importe">
<div class="input-group">
<span class="input-group-addon">$</span>
<input type="text" class="importe form-control" aria-label="Importe" name="importe" step="0.001" min="0.000" readonly="true" onchange="calcular_importe_total(this)">
</div>
</td>
<td data-title=""><button type="button" class="btn btn-danger btn-sm" onclick="borrar_renglon(this)">Cancelar</button></td>
</tr>
</tbody>
</table>
</div>
<div>
<label aria-label="Total Importe">Total Importe:</label>
<div class="input-group col-lg-4 pull-right">
<span class="input-group-addon">$</span>
<input class="form-control" id="importe_total" type="text" aria-label="Total Importe" readonly="true" >
</div>
</div>
<span class="row"></span>
<div class="panel-content">
<button type="button" class="btn btn-success btn-sm" onclick="nuevo_renglon()" aria-label="Agregar otro producto">+</button>
</div>
<br>
<div class="form-actions panel-footer">
<%= submit_tag("Generar", :class => "btn btn-danger") %>
</div>
<% end %>
请注意,我正在使用select2 v4,所以我不希望再次填充这些
答案 0 :(得分:0)
如果没有视图代码很难说,但在不成功的路径中,您需要将@factura
设置为params...
中的相同值。这样Rails将使用该数据呈现new
视图(假设您在视图中使用form_for @factura
)。