Problems updating a record with Ajax Rails

0

A small problem happens to me, more than a problem a bad browsing experience, when I update the record through Ajax, the original record is kept, and the updated one remains as an aggregate, when I should update the same cell, until I update the page the old record disappears, for a better understanding I explain it in 2 images:

In this picture I just edited a file, the table keeps the original record, although temporarily while the page does not load:

When the page is reloaded, the original record disappears and the edited one remains:

I guess it must be the way I updated the file, then I share my code, I hope you can help me:

update.js.erb

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

enterprise_tags_controller.rb

  def update

    respond_to do |format|
      if @enterprise_tag.update(enterprise_tag_params)
        format.html { redirect_to admin_dashboard_path, notice: 'enterprise_tag was successfully created.' }
        format.js   {}
        format.json { render json: @enterprise_tag, status: :created, location: @enterprise_tag }
      else
        format.html { render action: "edit" }
        format.json { render json: @enterprise_tag.errors, status: :unprocessable_entity }
      end
    end
  end

link_edit

<%= link_to edit_enterprise_tag_path(enterprise_tag), remote: true, :data => { :open => 'exampleModal1' }, class: "button tiny green" do %><i class="fi-pencil"></i><% end %>

index.html.erb

<table id="enterprise-tags-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>
    
asked by Hector Hernandez 02.01.2017 в 06:39
source

2 answers

0

Solution to the problem:

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

    
answered by 05.01.2017 / 02:51
source
0

Your problem is that you insist on using append and, as I told you in the answer to your previous problem , that function is to insert content, not to replace or to erase.
I suggest you review Jquery's documentation when you are not clear about what a feature specifically does. What you should use is the function html and implement the replacement similar to the answer I gave you in your other problem.

    
answered by 05.01.2017 в 01:33