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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:22:38+00:00 2026-06-14T15:22:38+00:00

I am trying to use select2 with my rails application but cannot get the

  • 0

I am trying to use select2 with my rails application but cannot get the result displayed in my views. I see the correct json coming in chrome console but cannot get the result displayed in the dropdown …

Could you please help me ?

Thanks :

Controllers :

def friends
  if params[:term]
    @users = User.where{ ( (first_name =~ my{"%#{params[:term]}%"}) | (last_name =~ my{"%#{params[:term]}%"}) | ( ((first_name.op('||', ' ')).op('||',last_name)) =~ my{"%#{params[:term]}%"}) | ( ((last_name.op('||', ' ')).op('||',first_name)) =~ my{"%#{params[:term]}%"}) ) & ( adult==false ) }
  end
  respond_to do |format|  
    format.html
    format.json { render :json =>{:results => @users.to_json(only: [:id, :first_name, :last_name]) }}
  end
end

My Javascript :

$("#teen_search").select2({
  placeholder: "Search for a teen",
  minimumInputLength: 2,
  ajax: {
    url: "/friends.json",
    dataType:'json',
    data: function (search, page) {
      return {
        term: search, // search term
        page_limit: 10,
      };
    },

    results: function (data, page) {
      return data;
    }
  },
  dropdownCssClass: "bigdrop"
});

I cannot figure what’s wrong, thanks for your help

I just edited the controller render_to json and the results in js to fix an error in the chrome console, I know I am close but still cannot get my user first_name and last_name displayed in the field …

  • 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-14T15:22:39+00:00Added an answer on June 14, 2026 at 3:22 pm

    So I ended up doing that :

    User_Controller :

    def friends
    if params[:term]
        @users = User.where{ ( (first_name =~ my{"%#{params[:term]}%"}) | (last_name =~ my{"%#{params[:term]}%"}) | ( ((first_name.op('||', ' ')).op('||',last_name)) =~ my{"%#{params[:term]}%"}) | ( ((last_name.op('||', ' ')).op('||',first_name)) =~ my{"%#{params[:term]}%"}) ) & ( adult==false ) }.select("id,first_name,last_name,avatar,uid")
        @user = @users.paginate(:page => params[:page], :per_page => params[:page_limit])
    end
    respond_to do |format|  
        format.html
        format.json { 
          render :json => {
            :users => @user,
            :total => @users.count,
            :links => { :self => @user.current_page , :next => @user.next_page}
        } 
      }
    end
    end
    

    In the JS Script :

        $(document).ready(function() {
        $("#teens_select2").select2({
            placeholder: "Search for a teen",
            minimumInputLength: 1,
            ajax: {
                url: "/friends.json",
                dataType: 'json',
                quietMillis: 300,
                data: function (search, page) {
                    return {
                        term: search,
                        page_limit: 10,
                        page: page,
                    };
                },
                results: function (data, page) {
                var more = (page * 10) < data.total;                
                  return {results: data.users, more: more};
                }
            },
            formatResult: movieFormatResult,
            formatSelection: movieFormatSelection, 
            dropdownCssClass: "bigdrop" // apply css that makes the dropdown taller
        });
    });
    

    Also formatResult and formatSelection are configured like that before you load the script :

        function movieFormatResult(user) {
        var markup = "<table class='movie-result'><tr>";
        if ( user.uid !== null) {
             markup += "<td class='movie-image'><img src='" + user.avatar + "'/></td>";
        }
        else if (user.uid == null) {
            var gravatar = user.avatar + 40
             markup += "<td class='movie-image'><img src='" + gravatar + "'/></td>";
         }
    
        markup += "<td class='movie-info'><div class='movie-title'>" + user.first_name +' '+ user.last_name + "</div>";
    
        markup += "</td></tr></table>"
        return markup;
    }
    
    function movieFormatSelection(user) {
        return (user.first_name + ' ' + user.last_name);
    }
    

    And the in the view :

    <input type ="hidden" id="teens_select2"></input>
    

    I really hope it can be useful.

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

Sidebar

Related Questions

In my rails application I am trying to use rails3-jquery-autocomplete gem. I included the
In my rails application i am trying to use the auto complete plugin.Its not
I'm trying to learn some Rails but I seem to get stuck on this
I am using ruby-1.8.7-p302/Rails 2.3.11. I am trying to use FQL (Facebook API) to
I'm trying to get Rails to capitalize the first character of a string, and
I'm about to use STI in my Rails 2.3 application. I've modeled it the
I'm trying to use tabifier in my rails app so I have: <%= stylesheet_link_tag
I am trying to use migrations to manage my SQL Server views. I found
I'm trying to use form_for and fields_for in Rails 3 to let me edit
I'm trying to use capybara with rspec to test my rails app. I've simplified

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.