2

Estoy intentando eliminar registros de mi tabla, sin embargo aunque los elimina no actualiza la lista con ajax:

enterprises_controller.rb

def destroy
    @enterprise_tag.destroy

    respond_to do |format|
      if @enterprise_tag.destroy
        format.html { redirect_to admin_dashboard_path, notice: "enterprise was remove successfully" }
        format.js { render partial: "list" }
      end
    end

end

destroy.js.erb [ ¿Falta algo qui? ]

<% if @enterprise_tag.errors.empty? %>
  $("#items").append("<%= escape_javascript(render partial: 'list') %>");
<% end %>

index.html.erb

<% if @enterprise_tags.any? %>
  <table id="items">
    <tbody>
      <tr>
        <td>Nombre</td>
        <td>Fecha de creacion</td>
        <td colspan="2">Opciones</td>
      </tr>
      <% @enterprise_tags.each do |enterprise_tag| %>
        <%= render partial: "enterprise_tags/list", locals: { enterprise_tag:enterprise_tag } %>
      <% end %>
    </tbody>
  </table>
  <% else %>
    <div class="callout small text-center">
      <span>No hay ningun registro disponible</span>
    </div>
<% end %>

_list.html.erb

<tr>
  <td><%= link_to enterprise_tag_path(enterprise_tag), method: :delete, data: { confirm: "¿Desea eliminar este registro?" }, remote: true do %><i class="fi-trash"></i><% end %></td>
</tr>

1 respuesta 1

0

En este código

<% if @enterprise_tag.errors.empty? %>
  $("#items").append("<%= escape_javascript(render partial: 'list') %>");
<% end %>

en realidad no estás borrando nada, de hecho append te sirve para agregar html a tus elementos, no para borrar. Una solución podría ser agregarles id a los tags tr de tu lista:

<tr id='enterprise-tag-<%= enterprise_tag.id %>'>
  <td><%= link_to enterprise_tag_path(enterprise_tag), method: :delete, data: { confirm: "¿Desea eliminar este registro?" }, remote: true do %><i class="fi-trash"></i><% end %></td>
</tr>

luego en tu destroy.js.erb, podrías borrar el tag específico basado en su id:

<% if @enterprise_tag.errors.empty? %>
  $("#enterprise-tag-<%= @enterprise_tag.id %>").remove();
<% end %>
2
  • Va perfecto Alter! Te agradezco muchisimo! Como ultima instancia Alter, sabes en que podria modificar aqui para este caso de mi update: es.stackoverflow.com/questions/41933/… el 4 ene. 2017 a las 4:38
  • @HectorHernandez si la respuesta te fue de utilidad, recuerda que puedes marcarla como aceptada. Voy a echarle una mirada a tu otra pregunta. Saludos el 5 ene. 2017 a las 0:15

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.