使用ajax Rails更新注册表的问题

时间:2017-01-04 21:06:31

标签: ruby-on-rails ajax

enter image description here

我正在更新注册表,但是当更新注册表时,更新记录的行被杂乱无章,这里是我的代码:

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>

1 个答案:

答案 0 :(得分:0)

这是解决方案= D

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