Использование Rails JQuery для автозаполнения, когда нет модели - PullRequest
2 голосов
/ 22 февраля 2012

jQuery docs заявляет, что должно быть две Модели, первая из которых - модель, которую вы хотите использовать для автозаполнения, вторая - модель, у которой есть первая Модель (и прикрепленная к вашей странице) , Однако у меня нет второй модели, только контроллер с видом, который имеет элементы формы, один из которых - автозаполнение ввода первой модели (место проведения).

Документы говорят, что я должен сделать

resources :parse do
get :autocomplete_venue_name, :on => :collection
end

Но это не сработает, поскольку у контроллера 'Parse' нет модели. Я попытался сделать статический маршрут, но мне нужно это: опция сбора. Как я могу обойти это требование?

EDIT: Ниже приведен код плагина rails + jquery для автозаполнения. Обратите внимание, что у меня есть ProductsController, но у меня нет модели Products, поэтому я не могу выполнить настройку route.rb:

Бренд класса

МОДЕЛЬ ПОИСКА ПО:

   create_table :brand do |t|
     t.column :name, :string
   end

КОНТРОЛЛЕР С ЖЕЛАЕМОЙ АВТОКОМПЛЕКТНОЙ ФОРМОЙ:

class ProductsController < Admin::BaseController
   autocomplete :brand, :name
end

routes.rb

resources :products do
   get :autocomplete_brand_name, :on => :collection
end

Этот подход работает, только если у меня есть модель Products , которой у меня нет. У меня есть контроллер, который отображает форму с автозаполнением ввода.

1 Ответ

1 голос
/ 26 марта 2012

Я использую плагин jquery multi auto complete, в котором вы можете выбрать один или несколько авто

полное значение, и если нет модели, она будет работать, например,

$ (документ) .ready (function () {

     var availableTags = [];
    <%@tags.each do |tag|%>
        availableTags.push('<%=tag.name%>');
    <%end%>

    $('#tags').multicomplete({
        source: availableTags
    });

});

в моем случае @tags = Tag.all

Но вы можете указать любые другие значения в действии контроллера. Чей модели не существует

Как в контроллере

@ tags = ["милый", "красота", "потрясающий"]

Код, который я использую для автозаполнения, указан ниже.

(function($, $a, $p) { // getting $p as a parameter doesn't require me to "var $p=..." and saves a two bytes ;-) ("var " versus ",x" in argument list [when minifier is shrinking variables])
    $p = $a.prototype;
    $.widget('ui.multicomplete', $a, {
        // Search for everything after the last "," instead of the whole input contents
        _search: function(value) {
            $p._search.call(this, value.match(/\s*([^,]*)\s*$/)[1]);
        },
        // Overwrite _trigger to make custom handling for events while still allowing user callbacks
        // Setting my own callbacks on this.options or binding using .bind() doesn't allow me to properly handle user callbacks, as this must be called AFTER the user callbacks are executed (which isn't possible by bind()ing when this.options[] is set)
        _trigger: function(type, event, data) {
            // call "real" triggers
            var ret = $p._trigger.apply(this, arguments);
            // When its select event, and user callback didn't return FALSE, do my handling and return false
            if (type == 'select' && ret !== false) {
                // When a selection is made, replace everything after the last "," with the selection instead of replacing everything
                var val = this.element.val();
                this.element.val(val.replace(/[^,]+$/, (val.indexOf(',') != -1 ? ' ' : '') + data.item.value + ', '));
                ret = false;
            }
            // Force false when its the focus event - parent should never set the value on focus
            return (type == 'focus' ? false : ret);
        },
        _create:function() {
            var self = this;
            // When menu item is selected and TAB is pressed focus should remain on current element to allow adding more values
            this.element.keydown(function(e) {
                self.menu.active && e.keyCode == $.ui.keyCode.TAB && e.preventDefault();
            });
            $p._create.call(this);
        },
        _initSource: function() {
            // Change the way arrays are handled by making autocomplete think the user sent his own source callback instead of an array
            // The way autocomplete is written doesn't allow me to do it in a prettier way :(
            if ($.isArray(this.options.source)) {
                var array = this.options.source, self = this;
                this.options.source = function(request, response) {
                    response(self.filter(array, request)); // Use our filter() and pass the entire request object so the filter can tell what's currently selected
                };
            }

            // call autocomplete._initSource to create this.source function according to user input
            $p._initSource.call(this);

            // Save a copy of current source() function, than new source() sets request.selected and delegate to original source
            var _source = this.source;
            this.source = function(request, response) {
                request.selected = this.element.val().split(/\s*,\s*/);
                request.selected.pop(); // don't include the term the user is currently writing as selected
                _source(request, response);
            };
            // TODO: instead of overwritting this.source, I can overwrite _search which is easier, but than I'll have to repeat jQuery-UI's code that might change
        },

        // Like $.ui.autocomplete.filter, but excludes items that are already selected
        filter: function(array, request) {
            return $.grep($a.filter(array, request.term), function(value) {
                return $.inArray(value, request.selected) == -1;
            });
        }
    });
})(jQuery, jQuery.ui.autocomplete);

Но вы должны включить некоторые jquery-автозаполненные js-файлы из jquery.UI. Загрузите оттуда файлы и включите их в проект.

jquery.ui.core

jquery.ui.widget

jquery.ui.position

jquery.ui.autocomplete

...