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

  • Home
  • SEARCH
  • 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 3497728
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:24:21+00:00 2026-05-18T12:24:21+00:00

I use rails 3.0.3 A javascript auto complete needs data like this { query:’Li’,

  • 0

I use rails 3.0.3

A javascript auto complete needs data like this

{
 query:'Li',
 suggestions:['Liberia','Libyan Arab Jamahiriya','Liechtenstein','Lithuania'],
 data:['LR','LY','LI','LT']
}

My action is

  def autocomplete
    @query = params[:query]
    @customers = Customer.where('firstname like ?', "%#{@query}%")
    render :partial => "customers/autocomplete.json"
  end

My view is

{
    query:'<%= @query %>',
    suggestions: <%= raw @customers.map{|c| "#{c.firstname} #{c.lastname}" } %>,
    data: <%= raw @customers.to_json %>
}

it returns

{
    query:'e',
    suggestions: ["customer 1", "customer 2"],
    data: [1, 3]
}

it’s not working because the data for suggestions/data should be between simple quote…

I cannot use the to_json method, because it’ll returns all the content of my object.

Any suggestion?

cheers

  • 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-18T12:24:21+00:00Added an answer on May 18, 2026 at 12:24 pm

    Note: this is way out of date, Jbuilder is by far a better option.


    There are two ways you can approach this. If you simply need a subset of the fields in an object, you can use :only or :except to exclude what you don’t want.

    @customer.to_json(:only => [:id, :name])
    

    in your example it looks like you need to return json in a specific format, so simply serializing an array of results won’t work. The easiest way to create a custom json response is with the Hash object:

    render :json => {
      :query => 'e',
      :suggestions => @customers.collect(&:name),
      :data => @customers.collect(&:id)
    }
    

    I’ve tried using partials to build json responses, but that doesn’t work nearly as well as simply using Hash to do it.


    Formatting the first and last names as a single string is something you are likely to do a lot in your views, I would recommend moving that to a function:

    class Customer < ActiveRecord::Base
      ...
      def name
        "#{first_name} #{last_name}"
      end
    
      def name=(n)
        first_name, last_name = n.split(' ', 2)
      end
    end
    

    Just some convenience functions that makes your life a little easier, and your controllers/views cleaner.

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

Sidebar

Related Questions

I would like to use create a rails route for a user's open id.
Is it architecturally sound to use Rails and Oracle? My concern is that Oracle
I'm trying to decide whether to use a Rails or a Django guru to
I am planning to use jquery in Rails instead of prototype. I am not
When should I use specs for Rails application and when Cucumber (former rspec-stories)? I
I use both ruby on rails and Java. I really enjoy using migrations when
In my rails app I use the validation helpers in my active record objects
In a Rails app sometimes you use a redirect in an action... redirect_to :controller
When dropping the use of rspec and rspec-rails plugins and switching to the gem
In rails, is it recommended to use form helpers? Internally, everything boils down to

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.