Reputation: 35
I was thinking how to go back from an individual listing page to the index page (with the previous page). It's pretty much like "go back" function. But I don't want to always go to the 1st page.
The indexed page has parameter of page. localhost:3000/listings?page=2
But inside each listing, the URL lacks the page number information. localhost:3000/listings/1
I was using rails 4.1.8 and Kaminari 0.16.3
List controller
def index
@listings = Listing.order("created_at DESC").page(params[:page])
end
Index page:
<%= paginate @listings %>
Show page partial:
<%= link_to 'Back', root_path %>
routes:
root 'listings#index'
How can I do this?
Upvotes: 1
Views: 1192
Reputation: 9278
One way to do this is by storing values in the session
def index
if params[:page]
session[:listing_index_page] = params[:page]
end
@listings = Listing.order("created_at DESC").page(session[:listing_index_page])
end
If the user navigates to listings/index
for the first time, there will be no page param and Kaminari should assume page 1. Subsequently navigating to page 2 will store 2 in the session.
If the user returns to the index page later (without an explicit page param) the value will be retrieved from the session.
Upvotes: 1