At what point is the bad data attribute used when making ajax calls in ruby ​​on rails?

My problem is with navigation links / infinite scroll.

I turned on the delete button for each microcell. When this button is pressed, an ajax call appears on the server, and the microcell is deleted and the div is removed from the DOM.

Let's say that on my first page there were micro-posts from 1 to 10, micro-posts from 11 to 20 on the second, etc. Let's say that I delete elements 1, 2 and 3 on the first page. Then scroll through the hefty one. The server will "recount" microcells; since I removed 3 from the previous list, it will return items 13 through 22. In other words, microposts 10, 11 and 12 do not appear on the first page or second.

After searching stackoverflow, I found a solution:

Whenever I delete a micropost from the list, I load the micropot, which appeared immediately after the last micropost on the page, and inserted it at the bottom.

As I am now with the fix:

By default, I display 10 microposts per page using the will_paginate gem. I need to know the identifier of any micropost displayed at any given time. I achieve this by storing it in the opening tag of the div for the container that contains each micro-rig:

<% @microposts.each do |m| %>
     <div class="postHolder" data-micropost-id="<%= m.id %>">

I refer to it the same way as in myicroposts.js file:

$(function() {

     $('.message_delete').on('ajax:success', function(event, data, status, xhr){
          console.log(data); //to check if json is working
          console.log($('.postHolder').last().data('micropost-id'));
     });
});

Here my controller kills the action:

def destroy
    micropost = Micropost.find(params[:id])
       respond_to do |format|   
         micropost.destroy
       format.json  { render :json => micropost } #just to check if json is working
    end
end

, , , jquery, .postHolder div , , , , , . , , .

, . railscasts , , , , .

, , . , , , .

+3
1

, . , , . , , reset (10).

10 ( , 3 , 4 2)

(3 * 10) - (4) == 26

. 3 micropost id 26

, , !

+1

All Articles