$(function() {
    $("#wine").autocomplete({
        source: function(request, response) {
            $.ajax({
                url: "/wine/wines",
                dataType: "json",
                data: {
                    q: request.term
                },
                success: function(data) {
                        response($.map(data.wines, function(item) {
                            return {
                                label: item.name,
                                value: item.name,
                                id: item.id
                            }
                        }))
                }

            })
        },
        minLength: 3,
        delay: 1,
        select: function(event, ui) {
            location.href = "/wine/show/id/" + ui.item.id;
        }
    });
});
