Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 在引发异常后保持字段已填充_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 4 在引发异常后保持字段已填充

Ruby on rails 4 在引发异常后保持字段已填充,ruby-on-rails-4,Ruby On Rails 4,我有以下控制器正在尝试捕获异常: def create status = 'ok' begin # do something dodgy @factura = Factura.execute_procedure :store_prueba, 1 flash[:notice] = "Éxito" rescue ActiveRecord::StatementInvalid flash[:notice] = nil

我有以下控制器正在尝试捕获异常:

  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.newfactura_参数或其他参数

以下是视图:

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

请注意,我使用的是select2v4,因此我不希望在那些

上再次填充。没有视图代码很难说,但是在您失败的路径中,您需要将@factura设置为与参数中的值相同。。。。这样,假设您在视图中使用form_for@factura,Rails将使用该数据呈现一个新视图。

我没有使用form_for标记,我使用form_标记,因为form_for是当您使用activerecord时,模型应该是这样的。然后它就不起作用了。现在,表单没有从控制器传递任何对象。这可能有助于: