Problems to delete a record with AJAX in Rails [duplicated]

0

At the moment of deleting a record with AJAX , it deletes it but does not update the table with the record removed, possibly maybe it is doing wrong the render or I need to specify something more to destroy.js , I share my code, I hope you can help me:

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 [I have doubts in this part]

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

Browser console

Rails server

    
asked by Hector Hernandez 01.01.2017 в 23:01
source

1 answer

0

You need to add a method in the destroy to eliminate with js when the ajax is executed.

add the use of js in the destroy method.

  

controller

   def destroy
        respond_to do |format|
          format.js
        end
      end

Add a class to enterprise_tag for the js to look for it. Important to add the row: true so that they can do the function of the ajax.

<%= link_to 'eliminar', enterprise_tag, method: :delete, data: { confirm: '¿Estas seguro?' }, :class=> "delete_",remote: true  %>

in the destroy.js.erb add the code that will find the tr closest to the item that you will destroy.

  

destroy.js.erb

$('.delete_').bind('ajax:success', function() {  
    $(this).closest('tr').fadeOut();
});  
    
answered by 02.01.2017 / 03:07
source