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 8080877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:32:59+00:00 2026-06-05T16:32:59+00:00

My rails project is paginating the HTML format but not the the others. Can

  • 0

My rails project is paginating the HTML format but not the the others. Can someone suggest a more elegant approach to dealing with the difference in getting the @contacts collection? Perhaps the pagination version should be a different method that only supports HTML?

def index
  if request.format.to_sym == :html
    @contacts = Contact.paginate(page: params[:page]).search(params[:search])
  else
    @contacts = Contact.search(params[:search])
  end      

  respond_to do |format|
    format.html { render html: @contacts }
    format.mobile { render mobile: @contacts }
    format.json { render json: @contacts }
    format.xml { render xml: @contacts.to_xml }
  end
end

My solution was to add paginate as a RESTful resource in routes.rb which automagically gives me the route helper method: paginate_contacts_path

resources :contacts do
  collection do
    get 'paginate'
  end
end

and to have a separate paginate method in ContactsController

def index
  @contacts = Contact.search(params[:search])

  respond_to do |format|
    format.html { render html: @contacts }
    format.mobile { render mobile: @contacts }
    format.json { render json: @contacts }
    format.xml { render xml: @contacts.to_xml }
  end
end

def paginate
  @contacts = Contact.paginate(page: params[:page]).search(params[:search])

  respond_to do |format|
    format.html
  end
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-05T16:33:00+00:00Added an answer on June 5, 2026 at 4:33 pm

    Strongly prefer a separate method because this creates inconstancies and makes the piece less testable. Also you would, in for example documentation, need to create exceptions.

    Another way is to handle this with some parameter. Now it suddenly (when you only change views) returns different data. That might look like an error for an unknown developer and might raise issues.

    So don’t do it magically, clear parameter or separate method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I running a rails project and I am displaying a textfield that can often
im working on a rails project version 3 and i can't figure out how
My rails project is deployed to tomcat with the help of warbler, but I
This is my first Rails project. For some reason, I can't post to one
I have a rails project, the views only consist with HTML.ERB files, my client
I'm comfortable with starting my own rails project but am trying to view and
I have a legacy Rails project (2.4) that used to work in Netbeans. But
In a non-rails project, I can just install activesupport standalone. When I need its
in a rails project i have to deal with lots of strings, that are
My personal rails project uses a few API's for which I store the API

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.