1

I have this in my controller:

  def selectmodels
    @brand = Brand.find_by_name(params[:brand_name])
    @models = Model.where(:brand_id => @brand.id)
    render json: @models
  end

The returned code is processed by:

$.ajax({
    url: '/selectmodels?brand_name=' + test, type: 'get', 
    dataType: 'json',
    processData: false,
    success: function(data) {
        if (data == "record_not_found") {
            alert("Record not found");
        }
        else {
            alert(data);
            $('#style_model_name').autocomplete({ source: data });
        }
    }
});

I would like to have the "data" keys and values loaded up into my autocomplete text-field.

Thanks!

UPDATE:

I receive the following:

[{"brand_id":1,"created_at":"2012-04-09T03:12:43Z","id":1,"name":"x","updated_at":"2012-04-09T03:12:43Z"},{"brand_id":1,"created_at":"2012-04-09T03:15:54Z","id":2,"name":"y","updated_at":"2012-04-09T03:15:54Z"},{"brand_id":1,"created_at":"2012-04-09T09:33:59Z","id":5,"name":"z","updated_at":"2012-04-09T09:33:59Z"}]
4
  • 2
    What do you get instead? Commented Apr 14, 2012 at 3:37
  • Well, the alert(data) causes an alert pop-up which just says "[object Object],[object Object],[object Object]" ... When I use the text-field the autocomplete is working because when I type 3 blank fields turn up.. but no words inside them. Thanks! Commented Apr 14, 2012 at 4:42
  • ..and actually, I don't think I need the keys to load into the autocomplete.. just the text values. Commented Apr 14, 2012 at 4:45
  • 1
    What do you receive exactly? You can use chrome network console (for example) to monitor network requests/responses. Commented Apr 14, 2012 at 4:46

1 Answer 1

2
$.ajax({
    url: '/selectmodels?brand_name=' + test, type: 'get', 
    dataType: 'json',
    processData: false,
    success: function(data) {
        if (data == "record_not_found") {
            alert("Record not found");
        }
        else {
            var source = [];
            for (i in data) {
                for (k in data[i]) {
                    source.push(data[i][k]);
                }
            }
            $('#style_model_name').autocomplete({ source: source });
        }
    }
});

Or if you only want certain fields:

$.ajax({
    url: '/selectmodels?brand_name=' + test, type: 'get', 
    dataType: 'json',
    processData: false,
    success: function(data) {
        if (data == "record_not_found") {
            alert("Record not found");
        }
        else {
            var source = [];
            for (i in data) {
                source.push(data[i]['name']);
                source.push(data[i]['id']);
                source.push(data[i]['brand_id']);
            }
            $('#style_model_name').autocomplete({ source: source });
        }
    }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.