0

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')

1 Answer 1

0

Have you added in your Post model post.rb self.per_page?

For example self.per_page = 10

3
  • I have that in my Group model, since I'm using model associations @group.posts.paginate(page: params[:page], per_page: 4).order('created_at DESC') but even then is that line of code necessary . I went ahead and tried it in both Group and Post models and still no effect. The problem is the URL works and it should be redirecting but since the actual post hasn't been loaded yet because of pagination, the anchor can't get there so maybe there is a javascript solution or something? Jun 4, 2016 at 18:44
  • In your controller in method update you cab try redirect_to @post instead of redirect_to group_path(@group, anchor: "#{@post.id}") Jun 4, 2016 at 18:50
  • still no luck. Need to find a way to get to the post anchor even though they are not fully loaded because will_paginate gem is implemented Jun 6, 2016 at 6:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.