0

enter image description here

I'm updating a registry, but when the registry is updated, the row of the updated record is disorganized, here my code:

index.html.erb

<table id="enterprises-items">
  <thead>
    <tr>
      <td>Nombre</td>
      <td>Email</td>
      <td>Categoria</td>
      <td>Municipio</td>
      <td class="text-left">...</td>
      <td colspan="2">Opciones</td>
    </tr>
  </thead>
  <tbody>
    <% @enterprises.each do |enterprise| %>
      <%= render partial: "enterprises/list", locals: { enterprise:enterprise } %>
    <% end %>
  </tbody>
</table>

update.js.erb

<% if @enterprise.errors.empty? %>
  $('#exampleModal1').foundation('close');
  $('#enterprise-<%= @enterprise.id %>').html("<%= escape_javascript(render partial: 'list', locals: { enterprise:@enterprise }) %>");
<% end %>

_list.html.erb

<tr id="enterprise-<%= enterprise.id %>">
  <td><% if enterprise.name.blank? %>No disponible<% else %><%= truncate(enterprise.name, length: 30) %><% end %></td>
  <td><% if enterprise.email.blank? %>No disponible<% else %><%= truncate(enterprise.email, length: 30) %><% end %></td>
  <td><% if enterprise.enterprise_tag_id.blank? %>No disponible<% else %><%= truncate(enterprise.enterprise_tag_id, length: 10) %><% end %></td>
  <td><% if enterprise.municipality_id.blank? %>No disponible<% else %><%= truncate(enterprise.municipality_id, length: 15) %><% end %></td>
  <td class="text-left">...</td>
  <td><%= link_to edit_enterprise_path(enterprise), remote: true, :data => { :open => 'exampleModal1' }, class: "button tiny green", id: "update_" do %><i class="fi-pencil"></i><% end %></td>
  <td><%= link_to enterprise, method: :delete, data: { confirm: "¿Desea eliminar este registro?" }, remote: true, class: "button tiny red" do %><i class="fi-trash"></i><% end %></td>
</tr>
  • You should use `replaceWith(...)` instead of `html(...)` in your `update.js.erb`. [Here's an explanation of the differences](http://stackoverflow.com/questions/730916/whats-the-difference-between-jquerys-replacewith-and-html) – AbM Jan 04 '17 at 21:49
  • Hi AbM, how could I compose the code using replaceWith()? Because I have problema composing this: $("enterprises-items").replaceWith("#enterprise-tag-<%= @enterprise_tag.id %>"); – Hector Hernandez Jan 05 '17 at 01:38
  • Thanks a lot! AbM, I can do that! with this: $("#enterprise-<%= @enterprise.id %>").replaceWith("<%= escape_javascript(render partial: 'list', locals: { enterprise:@enterprise }) %>"); – Hector Hernandez Jan 05 '17 at 01:46

1 Answers1

0

This is the solution =D

$("#enterprise-<%= @enterprise.id %>").replaceWith("<%= escape_javascript(render partial: 'list', locals: { enterprise:@enterprise }) %>");