获取记录并将其与.build关联时出错

时间:2017-06-11 01:28:37

标签: ruby-on-rails sql-server ruby ruby-on-rails-4

.build放入我想要的特定记录时出错:

@ruta = Ruta.find_by_IdRutas(166)

显示以下错误:

  

ActiveRecord :: StatementInvalid(TinyTds :: Error:无法将值NULL插入列'RutaID',表   'database.dbo.Continuidad';列不允许空值。 UPDATE   失败:EXEC sp_executesql N'UPDATE [continuidad] SET [RutaID] = @ 0   WHERE [continuidad]。[RutaID] = @ 1; SELECT @@ ROWCOUNT AS AffectedRows',   N'@ 0 int,@ 1 int',@ 0 = NULL,@ 1 = 166):    app / controllers / rutas_controller.rb:54:在`datos_ruta'

错误仅发生在具有与continuid表相关的记录的记录中,但是当它没有相关记录时,它不会显示错误。

这是我的index行动:

def index
  @ruta = Ruta.find_by_IdRutas(166)
  @ruta.build_continuid
end

这是索引视图中的form

<%= form_for(@ruta, :method => :put, remote: true, html: {class: "form-horizontal formulario-validado-update"}) do |f| %><!--ajax-->
  <div nohidden class="modal fade si" id="myupdateruta" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
          <h4 class="modal-title" id="myModalLabel">Editar Ruta</h4>
        </div>
        <div class="modal-body">
          <div class="modal-body">
            <div class="form-group">
              <%= f.label :Ruta, "Número de Ruta:", class: "control-label col-md-3" %>
              <div class="col-md-7">
                <%= f.text_field :Ruta, class: "form-control", required: "true" %>
              </div>
            </div>
            <%= f.fields_for :continuid do |continuid| %>
              <div class="form-group">
                <%= continuid.label :FolDevol, "Folio de devolución:", class: "control-label col-xs-5 col-sm-5 col-md-5" %>
                <div class="col-xs-5 col-sm-5 col-md-5">
                  <%= continuid.text_field :FolDevol, class: "form-control " %>
                </div>
              </div>
            <% end %>
          </div>
          <div class="modal-footer">
            <button type="button" id="myupdatebutton" class="btn btn-default" data-dismiss="modal">Cerrar</button>
            <%= submit_tag "Actualizar", class: "btn btn-primary", data: { disable_with: 'Actualizando' }%>
          </div>
        </div>
      </div>
    </div>
<% end %>

这是我的Ruta型号:

has_one :continuid, class_name: "Continuid", foreign_key: "RutaID"
accepts_nested_attributes_for :continuid

这是我的Continuid型号:

belongs_to :ruta, class_name:"Ruta", foreign_key: "RutaID"

我可能做错了什么?

0 个答案:

没有答案