简体   繁体   English

如何在不刷新页面的情况下通过分页将新数据添加到索引页面?

[英]How to add a new data to the index page with pagination without refreshing the page?

Post controller index is 后控制器索引为

<% if params[:topic_id].present? %>
<div class="new_post">
<%provide(:page_title,@topic.topic) %>
<h2><%= @topic.topic %></h2>
<%= link_to "Add Post",new_topic_post_path,remote: true,id: "post"%> . 
<br>
<% else %>
<h1>Posts</h1>
 <%provide(:page_title,"ALL") %>
<% end %>
<div class="row" id="posts">
<%= render @posts %>
</div>
<%= will_paginate @posts ,renderer: BootstrapPagination::Rails %>
</div>

My @post partial is 我的@post部分是

<div class="col-sm-6 col-lg-4" id="<%=post.id%>">
<div class="card">
<div class="card-topper">
  <%=image_tag post.image.url(:medium) %>
</div>
<div class="card-block">
 h4 class="card-title"><%= link_to post.title, topic_post_path(topic_id: post.topic_id,id: post.id),remote: true %><%= "(#{post.topic.topic})" %><br></h4>
  <p class="card-text" id="unused"><%= truncate(post.description,length:30) %></p>
  <%= "Avg.rate:#{post.ratings.average(:rate).to_f.round(2)}"%><br>
  <%= "#{post.comments_count} comments" %><br>

  <% if ReadStatus.where(user_id: current_user.id,post_id: post.id).blank? %>
    <%= link_to 'Read',topic_post_path(topic_id: post.topic_id,id: post.id),class: 'btn btn-read read' %>
  <% else %>
    <%= link_to 'Continue Reading',topic_post_path(topic_id: post.topic,id: post), class: 'btn btn-read read'%>
    <% end %>
</div>

Create Action in Post Controller is as follows 在Post Controller中创建操作如下

class PostsController < ApplicationController
protect_from_forgery
before_action :authenticate_user!
before_action :set_topic
before_action :set_post, only: [:show, :edit, :destroy, :update]
respond_to :html,:js
def create
@post = @topic.posts.new(post_params)
@post.user=current_user
if @post.save
  flash[:notice] = "Post sent Successfully"
  respond_to do |format|
    format.html {redirect_to topic_post_path(id: @post.id)}
    format.js
  end
else
  render 'new'
end
end

My create.js is 我的create.js是

$('#posts').prepend('<%= j render @post %>');
$("#<%= @post.id%>").hide().fadeIn(1000);

When i try to add a new post in my post index like this enter image description here 当我尝试在这样的帖子索引中添加新帖子时,请在此处输入图片描述

The new post is adding to the same page from where i create. 新帖子将添加到我创建的同一页面。 Here i set my per/page to 2 but the new post is adding to the same page which already has two post in the page. 在这里,我将我的每页设置为2,但是新帖子将添加到该页面中已经有两个帖子的同一页面。 But when i refresh it the index is loading in correct order. 但是当我刷新它时,索引以正确的顺序加载。 How can i get the pagination correct without the page refresh 如何在不刷新页面的情况下正确获得分页

first I would suggest you to stop firing sql queries from the views as it breaks the flow of MVC and it increases rendering time as well. 首先,我建议您停止从视图中触发sql查询,因为它破坏了MVC的流程,并且也增加了渲染时间。

Coming to the solution to the problem that you're facing: You're using prepend which is adding 3rd post in the space of 2 and it will keep on adding it if you're going to repeat the creation of another post. 来到您所面临的问题的解决方案:您正在使用prepend,它在2的间隔中添加了第3个帖子,并且如果您要重复创建另一个帖子,它将继续添加它。

I would suggest you to fetch @posts inside create method 我建议您在create方法中获取@posts

if @post.save
  #add line to fetch posts in the same order you're fetching on index page
  #example: 
  @posts = Post.order(created_at: :desc).paginate(:page => 1, :per_page => 2)
end

and in create.js.erb 并在create.js.erb中

$('#posts').empty();
<% @posts.each do |post| %> 
 $('#posts').append('<%= j render post %>');
<% end %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM