Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9149401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:28:18+00:00 2026-06-17T11:28:18+00:00

I need to use my own pagination code because my needs are too varied

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:28:19+00:00Added an answer on June 17, 2026 at 11:28 am

    I’d probably put this into the ApplicationController as a request filter

    class ApplicationController < ActionController::Base
      protected
      def set_paging_params
        @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
      end
    end
    
    class FooController < ApplicationController
      before_filter :set_paging_params, only: [:index]
    
      def index
        # do stuff with @per_page and others
      end
    end
    

    There’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.get implementation will support this). Something like this:

    class FooController < ApplicationController
      before_filter :set_paging_params, only: [:controller_action]
    
      def controller_action
        Item.get(params[:id], per_page: @per_page,
          page_num: @page_num,
          to_skip: @to_skip,
          max_index: @max_index,
        )
      end
    end
    
    class Item
      def self.get iid, opts = {}
        # use opts[:per_page] here
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need to use own imaged markers instead built-in pins. I have several questions. 1.
I need use this code: <%= button_tag :class => btn btn-primary do %> <%=
I'm writing some code in which I need to use my own HttpResponse object
I'm making a program for my own use. In this program, I need to
I need to use my own java class in a cfml page. This entry
I need help on how to change my code below to use my own
I need to create a gem, for my own use, that substitutes the current
I need use this method with three distinct classes: Orders, Customers, Suppliers public void
I have installed Matlab r2010a on my computer I need use the function xlsread
So, I need use this event so I can navigate trought blog posts. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.