I need to use my own pagination code because my needs are too varied and specific. I have this snippet copied and pasted in lots of controller actions
per_page = params[:per_page] ? params[:per_page].to_i : 15
page_num = params[:page] ? params[:page].to_i : 1
to_skip = ( page_num - 1 ) * (per_page)
max_index = per_page * page_num
The more i do this, the dumber I feel. I am sure there is a way to do this better, but I am not sure how.
BONUS (can i award bounty for this?) -> i would love to able to use the COMPUTED params in the model if i need to
Example:
# frontend requests for items 15-30
def controller_action
# code as above
# Item.get (...)
end
# and in the model have access to these params
def get
# use per_page, to_skip
end
I’d probably put this into the
ApplicationControlleras a request filterThere’s no way for model to use these controller instance variables, unless you pass them explicitly (or capture them in a closure, but I’m don’t know if your
Item.getimplementation will support this). Something like this: