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?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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
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.
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.
And of course, Model and Model.column stand for the model and column name for your application.