Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to paginate combined with anchor id

I'm very new to Ruby on Rails and learning while trying to fix some bugs on my company website. I'm trying to paginate a collection of records combined with a particular anchor, i.e. when user clicks on the next/previous page, the pagination happens and user lands on a particular section of the page. This is how my code looks at the moment:

view

<%= page_navigation_links @student_logs, :page %></p>

controller:

@student_logs.paginate(:page => params[:page], :per_page => 10)

application_helper

def page_navigation_links(pages, param_name=:page)

  will_paginate(pages, :class => 'pagination', :inner_window => 2, :outer_window => 0, :renderer => BootstrapHelper::LinkRenderer, :previous_label => '&larr;'.html_safe, :next_label => '&rarr;'.html_safe, :param_name => param_name)

end

This works fine for my pagination, but I need to fit a reference to an 'anchor id' into this code, like Student Logs, so that when user navigates to a different page the browser navigates the user to the heading with id: "student_logs".

I can't figure out how to do this. Can anyone help?

like image 804
Mahab Avatar asked Oct 29 '12 12:10

Mahab


1 Answers

I do not have direct experience with it. However, this link shows a way. I could not test but you can certainly do.

<%= will_paginate @posts, :params => {:anchor => i} %>

If this works, then you can utilize this way in your helper:

def page_navigation_links(pages, param_name=:page)
  will_paginate(pages, :params => {:anchor => "#ANCHOR"}, :class => 'pagination', :inner_window => 2, :outer_window => 0, :renderer => BootstrapHelper::LinkRenderer, :previous_label => '&larr;'.html_safe, :next_label => '&rarr;'.html_safe, :param_name => param_name)
end

You can replace #ANCHOR as you need.

like image 187
HungryCoder Avatar answered Nov 17 '22 06:11

HungryCoder