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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:40:01+00:00 2026-06-15T11:40:01+00:00

I am using this gem for autocomplete feature: https://github.com/argerim/select2-rails and select2. This is my

  • 0

I am using this gem for autocomplete feature:

https://github.com/argerim/select2-rails and select2.

This is my action:

def autocomplete_subject
   @messages = Message.where(:by_the_system => nil).page(params[:page]).per(params[:page_limit])
    messages = Array.new
    @messages.each do |m|
      element = Hash.new
      element[:id] = m.id
      element[:text] = [m.subject].join(' ')
      messages << element
    end
   results = Hash.new
   results[:results] = messages
   respond_to do |format| 
     format.json { 
      render :json => {
        :results => results,
        :total => @messages.count,
     } 
    }
  end
 end

This is my javascript:

function messageFormatSelection(message) {
  return message.subject;
}

$("#search_boxes #subject").select2({
        minimumInputLength: 3,
        multiple: true,
        width: "300px;",
        ajax: {
            url: "/admin/messages/autocomplete_subject_nil.json",
            dataType: 'json',
            quietMillis: 100,
            data: function (term, page) { // page is the one-based page number tracked by Select2
                return {
                    subject: term, //search term
                    page_limit: 5, // page size
                    page: page, // page number
                };
            },
            results: function (data, page) {
            var more = (page * 5) < data.total;                
              return {results: data.results, more: more};
            }
        },
        formatSelection: messageFormatSelection // omitted for brevity, see the source of this page
   });

This is my json view:

{"results":{"results":[{"id":"50b4f5c01d41c811fb000014","text":"Microfunc con id: 50ae41011d41c86ad8000004"},{"id":"50b4f8d31d41c811fb000026","text":"Re: Pedido con id: 50b4f6041d41c811fb000018"},{"id":"50b4fcf61d41c811fb00002e","text":"Pedido con id: 50b4f6041d41c811fb000018"},{"id":"50b4fe531d41c811fb000032","text":"Re: Pedido con id: 50b4f6041d41c811fb000018"},{"id":"50b4ff431d41c811fb000038","text":"Re: Pedido con id: 50b4f6041d41c811fb000018"},{"id":"50b4f5d71d41c811fb000017","text":"Re: Microfunc con id: 50ae41011d41c86ad8000004"},{"id":"50b4f8231d41c811fb000024","text":"Pedido con id: 50b4f6041d41c811fb000018"},{"id":"50b4fa921d41c811fb000028","text":"Re: Pedido con id: 50b4f6041d41c811fb000018"},{"id":"50b4fee41d41c811fb000034","text":"Re: Pedido con id: 50b4f6041d41c811fb000018"},{"id":"50b4ff291d41c811fb000036","text":"Pedido con id: 50b4f6041d41c811fb000018"},{"id":"50b4ffd71d41c811fb00003a","text":"mira os poneis deacuerdo o que"},{"id":"50b5013d1d41c811fb00004b","text":"Re: Microfunc con id: 50ae41011d41c86ad8000004"},{"id":"50b51ccb1d41c811fb000061","text":"mensaje enviado desde el panel de administraci\u00f3n"},{"id":"50b51e0c1d41c811fb000063","text":"Re: mensaje enviado desde el panel de administraci\u00f3n"},{"id":"50b51e461d41c811fb000065","text":"Microfunc con id: 50ae3b321d41c849c500000b"},{"id":"50b51e611d41c811fb000068","text":"Re: Microfunc con id: 50ae3b321d41c849c500000b"},{"id":"50b61b831d41c80b5d000004","text":"Re: mira os poneis deacuerdo o que"},{"id":"50b897a41d41c83613000034","text":"Microfunc con id: 50ae3b321d41c849c500000b"}]},"total":18}

I want to get the results sorted by the search and use infinite scroll feature, but it is not working properly.

What am I doing wrong?

  • 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-15T11:40:03+00:00Added an answer on June 15, 2026 at 11:40 am

    The sorting needs to be done server side, not on the client since the client doesn’t have access to all elements at load time. From your code it appears that text is equivalent to your Message subject. If so, you can simply modify this line:

    @messages = Message.where(:by_the_system => nil).
      page(params[:page]).per(params[:page_limit])
    

    to include a sort order:

    @messages = Message.where(:by_the_system => nil).
      order(:subject).
      page(params[:page]).per(params[:page_limit])
    

    Your response should include the current page, and whether this is the last page:

    respond_to do |format| 
     format.json { 
      render :json => {
        :results => results,
        :total => @messages.count,
        :page => params[:page].to_i,
        :last_page => @messages.last_page
     } 
    }
    

    You also need to modify your ajax request to update the page variable instead of an unused ‘more’ offset:

    results: function (data, page) {
              page = data.page + 1;
              if (data.last_page == data.page){ 
                // do something to disable searching as list is now exhausted
              }
              return {results: data.results};
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im using this gem to add private messages to my application. https://github.com/LTe/acts-as-messageable/blob/master/lib/acts-as-messageable/message.rb I`m trying
I am developing a Rails app using OmniAuth, OmniAuth-salesforce and this gem: https://github.com/heroku/databasedotcom I
I am using this gem for private messages: https://github.com/jongilbraith/simple-private-messages I want to create a
I am using this very nice LinkedIn ruby gem (https://github.com/pengwynn/linkedin) for a project of
I'm using this gem to access google docs in my app. https://github.com/gimite/google-drive-ruby I login
I'm using https://github.com/blueimp/jQuery-File-Upload to upload files to S3 on my Rails app (Installed this
I am using jtv-apns gem for push notifications to iOS : https://github.com/justintv/APNS This gem
I'm using auto_complete plugin: https://github.com/crowdint/rails3-jquery-autocomplete I have installed the gem and added require line
I am using this gem for payments in paypal https://github.com/tc/paypal_adaptive I am very confused
I'm using the delayed_job_web gem to monitor delayed jobs. https://github.com/ejschmitt/delayed_job_web It is accessible using

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.