简体   繁体   中英

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. 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. Meaning it doesn't redirect_to any posts after the four. 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.

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

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

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 ?

For example self.per_page = 10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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