Delete records with ajax rails

2

I'm trying to delete records from my table, but even if you delete them, you do not update the list with 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 [Missing something 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>
    
asked by Hector Hernandez 31.12.2016 в 19:31
source

1 answer

0

In this code

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

you are not really deleting anything, in fact append serves to add html to your elements, not to delete. One solution could be to add id to the tr tags in your list:

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

then in your destroy.js.erb , you could delete the specific tag based on your id:

<% if @enterprise_tag.errors.empty? %>
  $("#enterprise-tag-<%= @enterprise_tag.id %>").remove();
<% end %>
    
answered by 02.01.2017 / 18:23
source