Ruby on Rails 3 project. After updating a record we return to the index of all records (not a view of the updated record). The index is paged with Kaminari. How do we return to the page of the index that contains the updated record?
There is a similar question and answer for Java JPA/Hibernate Finding out the page containing a given record using JPA (Hibernate) that shows the sql needed to get the offset of the record in the index view. Something like
SELECT COUNT(*)
FROM Model r
WHERE r.column < ?
ORDER BY r.id
The page number will be offset/records_per_page + 1.
The question then is how to get the records_per_page from Kaminari? Kaminari lets you set the number of records per page in configuration, and for a specific model through Model.paginates_per. It provides the Model.default_per_page method to yield number of records per page for the model.
The best place to decide the page will be in the controller method that displays the index. That method already picks up the page param, if present, to display the correct page. Another way would be to compute the page before redirect and send the page param. Let's try the former.
After saving the edit we redirect to the index with a param identifying the record we want included in the index page redirect with params. The index sorts by Model.column.
redirect_to model_index_url(:for_column => @model_record.column),
:notice => 'Record saved'
Then in the index method of the controller we let the page param govern, or compute the page from the for_column param if present.
page = 1
if (params[:page])
page = params[:page]
elsif (params[:for_column])
offset = Model.select('count(*) as count').where('? < column',
params[:for_column]).order(id).first
page = offset.count/Model.default_per_page + 1
end
@records = Model.all.order('column desc').page(page)
And of course, Model and Model.column stand for the model and column name for your application.
© 2022 - 2024 — McMap. All rights reserved.