简体   繁体   English

Rails redirect_to锚点和will_paginate gem问题

[英]Rails redirect_to anchor and will_paginate gem issue

I have a posts controller and when I update my post, I redirect_to the specific post via an anchor tag. 我有一个帖子控制器,当我更新自己的帖子时,我会通过一个定位标记重定向到特定的帖子。 I am using the will_paginate gem for my posts. 我在帖子中使用will_paginate gem。 I paginate every four posts, so what is happening is the redirect to anchor tag works for the first four posts listed, but it will not work for any additional posts after the four because of pagination, even though the correct URL is displayed. 我每四个帖子分页一次,所以发生的是重定向到anchor tag的操作对列出的前四个帖子有效,但是由于分页,即使显示了正确的URL,它也不适用于四个之后的任何其他帖子。 Meaning it doesn't redirect_to any posts after the four. 这意味着它不会将redirect_to重定向到四个之后的任何帖子。 If I change will_paginate to paginate every ten for example, then it will correctly list the URL for the 11th if that is the one I update, but since the posts beyond ten aren't loaded, the html/anchor does not display. 例如,如果我将will_paginate更改为每10个分页,则它将正确列出第11个URL(如果我更新的是URL),但是由于未加载超过10个的帖子,因此不会显示html / anchor。

URL 网址

http://localhost:3000/groups/16#5 CORRECT URL BUT DOESN'T LOAD THE ADDITIONAL COMMENTS AND DISPLAY

Posts controller 帖子控制器

def update
  @group = Group.find(params[:group_id])
  if @post.update(post_params)
    redirect_to group_path(@group, anchor: "#{@post.id}")
  else
    render :edit
  end
end

Post _post.html.erb 发布_post.html.erb

...
<p class="postParagraph" id="<%= post.id %>"><%= post.caption %> </p>

will_paginate will_paginate

@posts = @group.posts.paginate(page: params[:page], per_page: 4).order('created_at DESC')

Have you added in your Post model post.rb self.per_page ? 您是否在Post模型中添加了post.rb self.per_page

For example self.per_page = 10 例如self.per_page = 10

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

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