How can I use 'in-page link' by # when using Kaminari? - ruby-on-rails

I have this in my view
<%= paginate #comments, :window => 4, :outer_window => 2 %>
This automatically creates pagination links. If I click one of them, it takes me to
http://example.com/shop/walmart?page=2
How can I add in-page link, which takes me to this url?
http://example.com/shop/walmart?page=2#abc
Supposing #abc is the destination here.

<%= paginate #comments, :window => 4, :outer_window => 2 , :params => {:anchor => "abc"} %>

Related

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 => '←'.html_safe, :next_label => '→'.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?
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 => '←'.html_safe, :next_label => '→'.html_safe, :param_name => param_name)
end
You can replace #ANCHOR as you need.

Rails will_paginage

i am using will_paginate for pagination,
When i reach the last page and then click on Next link its again taking me to first page,
Actually i want to disable the Next button when i reach the last page.
please advise me how to solve this..
this is the code i used for pagination
<%= will_paginate #users,
:prev_label => "Preevious",
:next_label => "Next",
:page_links =>true,
:renderer => PaginationListLinkRenderer
%>
Controller:
#users = User.paginate :per_page => 5,
:page => params[:page],
:order => 'name ASC',
:conditions => ""
View:
<%= will_paginate #users,
:previous_label => "Previous",
:next_label => "Next",
:inner_window => "2",
:outer_window => "0" %>
Above is all my code and it works for me in will_paginate. Once reached down the last page, the "Next" link change to bolded text, so as the last page number (see image below).
I am using
<%= will_paginate #users %>
and it is working fine. It disables the next link when i reach the last page.

Using link_to with an instance variable and arguments

I am trying to render a comments partial to use with both the blog and video models. Here's the blog show page asking for the comments partial and passing #blog as the model (I will pass #video on the video's show page):
<%= render :partial => 'comments/comments', :locals => {:model => #blog} %>
This next code is to order the comments as newest first/oldest first:
<% if #comments.count > 1 %>
<span class="list_order">
<%= link_to('Newest First', model, :order => "DESC", :anchor => "comments") + " | " +
link_to('Oldest First', model, :order => "ASC", :anchor => "comments") %>
</span>
<% end -%>
This works fine when I say:
link_to('Newest First', blog_path(#blog, :order => "DESC".... etc.)
But I know that you can also just pass:
link_to('Newest First', #blog)
and it will automatically go to the blog show page. So in my code, I'm passing the "model" local, and it does refresh the page, but it does not take my argument for :order or :anchor. How do you pass arguments when using only the instance variable rather than the path on the link_to method?
Ok, I finally got the chance to ask a friend of mine and found the solution. I needed to use polymorphic paths. So in my example above, the following code works:
<%= link_to('Newest First', polymorphic_path(model, :order => "DESC", :anchor => "comments") + " | " + link_to('Oldest First', polymorphic_path(model, :order => "ASC", :anchor => "comments") %>
Then it knows to generate the right path for the variable used. Here's some info on that:
http://apidock.com/rails/ActionDispatch/Routing/PolymorphicRoutes/polymorphic_path

Will Paginate: How change default page in links

I have two partial. The first have a common will_paginate but in the second will_paginate I need to change the links (default url) generated by will_paginate.
Please I need their answer.
Thanks
You can use :param_name to change the query-string parameter:
<%= will_paginate #posts, :param_name => :posts_page %>
<%= will_paginate #comments, :param_name => :comments_page %>
Note that in your controller you must also change this:
#posts = Post.paginate :page => params[:posts_page]
#comments = Comment.paginate :page => params[:comments_page]
Try this
will_paginate(#some_collection, :params => { :controller => "foo", :action => "bar" })

Using partials on arrays?

I'm confused by how partials behave with respect to arrays.
Simple Example
I have the following in a view:
render :partial => "foobars", :object => [1, 2, 3]
And in _foobars.html.erb, I have
<%= foobars.size %>
<%= foobars[0] %>
The weird thing is that what gets displayed is "444" and "101", not "3" and "1".
Is something special happening because I'm passing in an array?
What Jed says works but what you are looking for is really
render :partial => "foobars", :collection => [1,2,3]
Inside the partial, the iteration will happen by itself on the passed array and foobars will hold the array element of each iteration
<%= foobars %>
will give 1, 2 and 3 inside the partial.
I think what you want is:
render :partial => "foobars", :locals => {:object => [1, 2, 3]}
and inside the partial
<%= object.size %>
<%= object[0] %>

Resources