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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:16:02+00:00 2026-05-24T13:16:02+00:00

I’m running a ruby on rails app, and I’m using jQuery to show some

  • 0

I’m running a ruby on rails app, and I’m using jQuery to show some autocomplete suggestions in a form. The autocomplete suggestions are sourced from 2 different arrays which I merge into one, and then display.

Now, the issue is that I need to display results from one file in a different manner than results from another. How do I do this? That is, they will all be in the same autocomplete box, but the results from one may have a different font, color, etc. (a separate CSS hook?)

autocomplete.js:

$(function() {
  $('.autocomplete_field').autocomplete({
  source: '/locations/autocomplete.js'
  });
});

locations_controller.rb:

  def autocomplete
    respond_to do |format|
      format.js do
        client = GooglePlacesAutocomplete::Client.new(:api_key => "mykey")
        locations = client.autocomplete(:input => params[:term], :lat => "x", :lng => "y", :radius => "25000")
        add = Location.all.map { |l| { :label => l.address, :value => l.address } }
        locations = locations.predictions.map { |l| {:label => l.description, :value => l.description} }
        final = add + locations
        render :json => final
      end
    end
  end

As you can see, the locations_controller simply renders a JSON object which is the two concatenated arrays to the URL /locations/autocomplete.js, which the autocomplete.js renders.

I realize I may need to modify my controller to return 2 separate JSON objects, one for add, and one for location. I can do this, but I do not know how to merge them together in the final autocomplete, and how to format the queries from them differently.

I’m very inexperienced in javascript/jquery, so I’d appreciate an answer which explains those components well, even if you gloss over the required changes to the Rails app.

This is what it boils down to:

When I view the HTML for the autocomplete box, each autocomplete item is as follows:

<li class="ui-menu-item" role="menuitem">
<a class="ui-corner-all" tabindex="-1">(autocomplete suggestion)</a>
</li>

I simply need to add an additional class besides “ui-menu-item” and “ui-corner-all” to the results obtained from one JSON, which will not be present in the results obtained from the other.

Now, I see here that its possible to add an additional class by modifying the .renderItem function, but how do I do apply this to every element in a certain JSON object (js approach), or apply this to every element in a ruby array beforehand (through rails)?

  • 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-05-24T13:16:03+00:00Added an answer on May 24, 2026 at 1:16 pm

    You’re correct about having to use the _renderItem function. With this method you can apply any class you want to the li or underlying a as long as you include the data that the widget expects.

    I would simply send down another variable from your ruby code (sorry, not versed in ruby at all) that designates whether this is an address or not (or whatever property you want to branch on to change the formatting).

    Then, you would write the _renderItem function like this:

    $("#auto").autocomplete({
        source: source
    }).data("autocomplete")._renderItem = function(ul, item) {
        var listItem = $("<li></li>")
            .data("item.autocomplete", item)
            .addClass(item.isAddress ? "address" : "landmark");
    
        if (item.isAddress) {
            listItem.html("<a>" + $.map(item.label.split(","), function(el) {
                return "<span class='addressline'>" + el + "</span>";
            }).join('') + "</a>");
        } else {
            listItem.html("<a>" + item.label + "</a>");
        }
    
        return listItem.appendTo(ul);
    
    };
    

    Assuming you’re passing down an isAddress value for those objects in your array that are an address. Keep in mind that you can pass down any data you want as long as the widget has a label or value field (or both).

    Here’s a more complete example with a local data source: http://jsfiddle.net/andrewwhitaker/tW5zE/

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

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
For some reason, after submitting a string like this Jack’s Spindle from a text
I am currently running into a problem where an element is coming back from
I have a text area in my form which accepts all possible characters from
I am using Paperclip to handle profile photo uploads in my app. They upload
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.